/* * Copyright (c) 2015, The Linux Foundation. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of The Linux Foundation nor * the names of its contributors may be used to endorse or promote * products derived from this software without specific prior written * permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ #define VERBOSE 1 #include #include #include #include #include #include "meta_format.h" #define MAX_IMAGES 16 meta_header_t meta_header; img_header_entry_t img_header[MAX_IMAGES]; enum MetaImage { undefined, bootloader, radio }; enum MetaImage meta_image = undefined; struct device_info di; void usage() { printf("\nUsage:\n"); printf("meta_image ... -o -v \n"); printf("eg: meta_image sbl1 sbl1.mbn rpm rpm.mbn tz tz.mbn hyp hyp.mbn aboot emmc_appsboot.mbn -o bootloader.img -v M8916AAAAAULGD21210017.1\n"); } int read_devinfo () { FILE *fin = fopen("devinfo.bin", "rb"); fread(&di, sizeof(di), 1, fin); fclose(fin); } int write_devinfo () { FILE *fout; /* If image exists, just update it */ read_devinfo(); /* No devinfo.bin, create new one */ strncpy(di.magic, DEVICE_MAGIC, sizeof(DEVICE_MAGIC)); di.is_unlocked = 1; di.is_tampered = 0; di.is_verified = 0; di.charger_screen_enabled = 1; memset(di.display_panel, 0, sizeof(di.display_panel)); if (meta_image == bootloader) memcpy(di.bootloader_version, meta_header.img_version, sizeof(di.bootloader_version)); else if (meta_image == radio) memcpy(di.radio_version, meta_header.img_version, sizeof(di.radio_version)); fout = fopen("devinfo.bin", "wb"); fwrite(&di, sizeof(di), 1, fout); fclose(fout); } int extract_images (char *file_path) { FILE *fp; FILE *fout; void *buffer; int i, images = 0; #if VERBOSE printf ("Extracting images...\n"); #endif if(strstr(file_path, "bootloader")) meta_image = bootloader; else meta_image = radio; if(file_path != NULL) { fp = fopen(file_path, "rb"); fread(&meta_header, sizeof(meta_header), 1, fp); fread(&img_header, sizeof(img_header), 1, fp); #if VERBOSE printf("Image version: %s\n", meta_header.img_version); #endif images = meta_header.img_hdr_sz / sizeof(img_header_entry_t); for(i=0; i