From 253c18d2fbab6c16886af8bdfd528df3ebf82b93 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 18 Mar 2015 22:47:09 -0700 Subject: Prepare for switching to libziparchive. Bug: 19765860 Change-Id: I58249d28fbc4975428d0dcac5107d1994c35676c --- fastboot/Android.mk | 5 +- fastboot/bootimg.c | 87 --- fastboot/bootimg_utils.cpp | 85 +++ fastboot/bootimg_utils.h | 49 ++ fastboot/fastboot.c | 1265 -------------------------------------------- fastboot/fastboot.cpp | 1235 ++++++++++++++++++++++++++++++++++++++++++ fastboot/fastboot.h | 10 + fastboot/fs.h | 8 + fastboot/usb.h | 8 + 9 files changed, 1398 insertions(+), 1354 deletions(-) delete mode 100644 fastboot/bootimg.c create mode 100644 fastboot/bootimg_utils.cpp create mode 100644 fastboot/bootimg_utils.h delete mode 100644 fastboot/fastboot.c create mode 100644 fastboot/fastboot.cpp (limited to 'fastboot') diff --git a/fastboot/Android.mk b/fastboot/Android.mk index aa5b14a..9cb002f 100644 --- a/fastboot/Android.mk +++ b/fastboot/Android.mk @@ -19,10 +19,11 @@ include $(CLEAR_VARS) LOCAL_C_INCLUDES := $(LOCAL_PATH)/../mkbootimg \ $(LOCAL_PATH)/../../extras/ext4_utils \ $(LOCAL_PATH)/../../extras/f2fs_utils -LOCAL_SRC_FILES := protocol.c engine.c bootimg.c fastboot.c util.c fs.c +LOCAL_SRC_FILES := protocol.c engine.c bootimg_utils.cpp fastboot.cpp util.c fs.c LOCAL_MODULE := fastboot LOCAL_MODULE_TAGS := debug -LOCAL_CFLAGS += -std=gnu99 -Werror +LOCAL_CONLYFLAGS += -std=gnu99 +LOCAL_CFLAGS += -Wall -Werror ifeq ($(HOST_OS),linux) LOCAL_SRC_FILES += usb_linux.c util_linux.c diff --git a/fastboot/bootimg.c b/fastboot/bootimg.c deleted file mode 100644 index 240784f..0000000 --- a/fastboot/bootimg.c +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * 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. - * - * 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 AND FITNESS - * FOR A PARTICULAR PURPOSE 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. - */ - -#include -#include -#include - -#include - -void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline) -{ - strcpy((char*) h->cmdline, cmdline); -} - -boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, unsigned kernel_offset, - void *ramdisk, unsigned ramdisk_size, unsigned ramdisk_offset, - void *second, unsigned second_size, unsigned second_offset, - unsigned page_size, unsigned base, unsigned tags_offset, - unsigned *bootimg_size) -{ - unsigned kernel_actual; - unsigned ramdisk_actual; - unsigned second_actual; - unsigned page_mask; - boot_img_hdr *hdr; - - page_mask = page_size - 1; - - kernel_actual = (kernel_size + page_mask) & (~page_mask); - ramdisk_actual = (ramdisk_size + page_mask) & (~page_mask); - second_actual = (second_size + page_mask) & (~page_mask); - - *bootimg_size = page_size + kernel_actual + ramdisk_actual + second_actual; - - hdr = calloc(*bootimg_size, 1); - - if(hdr == 0) { - return hdr; - } - - memcpy(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE); - - hdr->kernel_size = kernel_size; - hdr->ramdisk_size = ramdisk_size; - hdr->second_size = second_size; - - hdr->kernel_addr = base + kernel_offset; - hdr->ramdisk_addr = base + ramdisk_offset; - hdr->second_addr = base + second_offset; - hdr->tags_addr = base + tags_offset; - - hdr->page_size = page_size; - - - memcpy(hdr->magic + page_size, - kernel, kernel_size); - memcpy(hdr->magic + page_size + kernel_actual, - ramdisk, ramdisk_size); - memcpy(hdr->magic + page_size + kernel_actual + ramdisk_actual, - second, second_size); - return hdr; -} diff --git a/fastboot/bootimg_utils.cpp b/fastboot/bootimg_utils.cpp new file mode 100644 index 0000000..d8905a6 --- /dev/null +++ b/fastboot/bootimg_utils.cpp @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * 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. + * + * 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 AND FITNESS + * FOR A PARTICULAR PURPOSE 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. + */ + +#include "bootimg_utils.h" + +#include +#include +#include + +void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline) +{ + strcpy((char*) h->cmdline, cmdline); +} + +boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, unsigned kernel_offset, + void *ramdisk, unsigned ramdisk_size, unsigned ramdisk_offset, + void *second, unsigned second_size, unsigned second_offset, + unsigned page_size, unsigned base, unsigned tags_offset, + unsigned *bootimg_size) +{ + unsigned kernel_actual; + unsigned ramdisk_actual; + unsigned second_actual; + unsigned page_mask; + + page_mask = page_size - 1; + + kernel_actual = (kernel_size + page_mask) & (~page_mask); + ramdisk_actual = (ramdisk_size + page_mask) & (~page_mask); + second_actual = (second_size + page_mask) & (~page_mask); + + *bootimg_size = page_size + kernel_actual + ramdisk_actual + second_actual; + + boot_img_hdr* hdr = reinterpret_cast(calloc(*bootimg_size, 1)); + if (hdr == 0) { + return hdr; + } + + memcpy(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE); + + hdr->kernel_size = kernel_size; + hdr->ramdisk_size = ramdisk_size; + hdr->second_size = second_size; + + hdr->kernel_addr = base + kernel_offset; + hdr->ramdisk_addr = base + ramdisk_offset; + hdr->second_addr = base + second_offset; + hdr->tags_addr = base + tags_offset; + + hdr->page_size = page_size; + + + memcpy(hdr->magic + page_size, + kernel, kernel_size); + memcpy(hdr->magic + page_size + kernel_actual, + ramdisk, ramdisk_size); + memcpy(hdr->magic + page_size + kernel_actual + ramdisk_actual, + second, second_size); + return hdr; +} diff --git a/fastboot/bootimg_utils.h b/fastboot/bootimg_utils.h new file mode 100644 index 0000000..b1a86cd --- /dev/null +++ b/fastboot/bootimg_utils.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * 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. + * + * 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 AND FITNESS + * FOR A PARTICULAR PURPOSE 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. + */ + +#ifndef _FASTBOOT_BOOTIMG_UTILS_H_ +#define _FASTBOOT_BOOTIMG_UTILS_H_ + +#include + +#if defined(__cplusplus) +extern "C" { +#endif + +void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline); +boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, unsigned kernel_offset, + void *ramdisk, unsigned ramdisk_size, unsigned ramdisk_offset, + void *second, unsigned second_size, unsigned second_offset, + unsigned page_size, unsigned base, unsigned tags_offset, + unsigned *bootimg_size); + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/fastboot/fastboot.c b/fastboot/fastboot.c deleted file mode 100644 index fc544a6..0000000 --- a/fastboot/fastboot.c +++ /dev/null @@ -1,1265 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * 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. - * - * 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 AND FITNESS - * FOR A PARTICULAR PURPOSE 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 _LARGEFILE64_SOURCE - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include "fastboot.h" -#include "fs.h" - -#ifndef O_BINARY -#define O_BINARY 0 -#endif - -#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a))) - -char cur_product[FB_RESPONSE_SZ + 1]; - -void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline); - -boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, unsigned kernel_offset, - void *ramdisk, unsigned ramdisk_size, unsigned ramdisk_offset, - void *second, unsigned second_size, unsigned second_offset, - unsigned page_size, unsigned base, unsigned tags_offset, - unsigned *bootimg_size); - -static usb_handle *usb = 0; -static const char *serial = 0; -static const char *product = 0; -static const char *cmdline = 0; -static unsigned short vendor_id = 0; -static int long_listing = 0; -static int64_t sparse_limit = -1; -static int64_t target_sparse_limit = -1; - -unsigned page_size = 2048; -unsigned base_addr = 0x10000000; -unsigned kernel_offset = 0x00008000; -unsigned ramdisk_offset = 0x01000000; -unsigned second_offset = 0x00f00000; -unsigned tags_offset = 0x00000100; - -enum fb_buffer_type { - FB_BUFFER, - FB_BUFFER_SPARSE, -}; - -struct fastboot_buffer { - enum fb_buffer_type type; - void *data; - unsigned int sz; -}; - -static struct { - char img_name[13]; - char sig_name[13]; - char part_name[9]; - bool is_optional; -} images[] = { - {"boot.img", "boot.sig", "boot", false}, - {"recovery.img", "recovery.sig", "recovery", true}, - {"system.img", "system.sig", "system", false}, - {"vendor.img", "vendor.sig", "vendor", true}, -}; - -void get_my_path(char *path); - -char *find_item(const char *item, const char *product) -{ - char *dir; - char *fn; - char path[PATH_MAX + 128]; - - if(!strcmp(item,"boot")) { - fn = "boot.img"; - } else if(!strcmp(item,"recovery")) { - fn = "recovery.img"; - } else if(!strcmp(item,"system")) { - fn = "system.img"; - } else if(!strcmp(item,"vendor")) { - fn = "vendor.img"; - } else if(!strcmp(item,"userdata")) { - fn = "userdata.img"; - } else if(!strcmp(item,"cache")) { - fn = "cache.img"; - } else if(!strcmp(item,"info")) { - fn = "android-info.txt"; - } else { - fprintf(stderr,"unknown partition '%s'\n", item); - return 0; - } - - if(product) { - get_my_path(path); - sprintf(path + strlen(path), - "../../../target/product/%s/%s", product, fn); - return strdup(path); - } - - dir = getenv("ANDROID_PRODUCT_OUT"); - if((dir == 0) || (dir[0] == 0)) { - die("neither -p product specified nor ANDROID_PRODUCT_OUT set"); - return 0; - } - - sprintf(path, "%s/%s", dir, fn); - return strdup(path); -} - -static int64_t file_size(int fd) -{ - struct stat st; - int ret; - - ret = fstat(fd, &st); - - return ret ? -1 : st.st_size; -} - -static void *load_fd(int fd, unsigned *_sz) -{ - char *data; - int sz; - int errno_tmp; - - data = 0; - - sz = file_size(fd); - if (sz < 0) { - goto oops; - } - - data = (char*) malloc(sz); - if(data == 0) goto oops; - - if(read(fd, data, sz) != sz) goto oops; - close(fd); - - if(_sz) *_sz = sz; - return data; - -oops: - errno_tmp = errno; - close(fd); - if(data != 0) free(data); - errno = errno_tmp; - return 0; -} - -static void *load_file(const char *fn, unsigned *_sz) -{ - int fd; - - fd = open(fn, O_RDONLY | O_BINARY); - if(fd < 0) return 0; - - return load_fd(fd, _sz); -} - -int match_fastboot_with_serial(usb_ifc_info *info, const char *local_serial) -{ - if(!(vendor_id && (info->dev_vendor == vendor_id)) && - (info->dev_vendor != 0x18d1) && // Google - (info->dev_vendor != 0x8087) && // Intel - (info->dev_vendor != 0x0451) && - (info->dev_vendor != 0x0502) && - (info->dev_vendor != 0x0fce) && // Sony Ericsson - (info->dev_vendor != 0x05c6) && // Qualcomm - (info->dev_vendor != 0x22b8) && // Motorola - (info->dev_vendor != 0x0955) && // Nvidia - (info->dev_vendor != 0x413c) && // DELL - (info->dev_vendor != 0x2314) && // INQ Mobile - (info->dev_vendor != 0x0b05) && // Asus - (info->dev_vendor != 0x0bb4)) // HTC - return -1; - if(info->ifc_class != 0xff) return -1; - if(info->ifc_subclass != 0x42) return -1; - if(info->ifc_protocol != 0x03) return -1; - // require matching serial number or device path if requested - // at the command line with the -s option. - if (local_serial && (strcmp(local_serial, info->serial_number) != 0 && - strcmp(local_serial, info->device_path) != 0)) return -1; - return 0; -} - -int match_fastboot(usb_ifc_info *info) -{ - return match_fastboot_with_serial(info, serial); -} - -int list_devices_callback(usb_ifc_info *info) -{ - if (match_fastboot_with_serial(info, NULL) == 0) { - char* serial = info->serial_number; - if (!info->writable) { - serial = "no permissions"; // like "adb devices" - } - if (!serial[0]) { - serial = "????????????"; - } - // output compatible with "adb devices" - if (!long_listing) { - printf("%s\tfastboot\n", serial); - } else if (strcmp("", info->device_path) == 0) { - printf("%-22s fastboot\n", serial); - } else { - printf("%-22s fastboot %s\n", serial, info->device_path); - } - } - - return -1; -} - -usb_handle *open_device(void) -{ - static usb_handle *usb = 0; - int announce = 1; - - if(usb) return usb; - - for(;;) { - usb = usb_open(match_fastboot); - if(usb) return usb; - if(announce) { - announce = 0; - fprintf(stderr,"< waiting for device >\n"); - } - usleep(1000); - } -} - -void list_devices(void) { - // We don't actually open a USB device here, - // just getting our callback called so we can - // list all the connected devices. - usb_open(list_devices_callback); -} - -void usage(void) -{ - fprintf(stderr, -/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */ - "usage: fastboot [