void do_update(char *fn, int erase_first) { void *zdata; unsigned zsize; void *data; unsigned sz; zipfile_t zip; queue_info_dump(); fb_queue_query_save("product", cur_product, sizeof(cur_product)); zdata = load_file(fn, &zsize); if (zdata == 0) die("failed to load '%s': %s", fn, strerror(errno)); zip = init_zipfile(zdata, zsize); if(zip == 0) die("failed to access zipdata in '%s'"); data = unzip_file(zip, "android-info.txt", &sz); if (data == 0) { char *tmp; /* fallback for older zipfiles */ data = unzip_file(zip, "android-product.txt", &sz); if ((data == 0) || (sz < 1)) { die("update package has no android-info.txt or android-product.txt"); } tmp = malloc(sz + 128); if (tmp == 0) die("out of memory"); sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data); data = tmp; sz = strlen(tmp); } setup_requirements(data, sz); data = unzip_file(zip, "boot.img", &sz); if (data == 0) die("update package missing boot.img"); do_update_signature(zip, "boot.sig"); if (erase_first && needs_erase("boot")) { fb_queue_erase("boot"); } fb_queue_flash("boot", data, sz); data = unzip_file(zip, "recovery.img", &sz); if (data != 0) { do_update_signature(zip, "recovery.sig"); if (erase_first && needs_erase("recovery")) { fb_queue_erase("recovery"); } fb_queue_flash("recovery", data, sz); } data = unzip_file(zip, "system.img", &sz); if (data == 0) die("update package missing system.img"); do_update_signature(zip, "system.sig"); if (erase_first && needs_erase("system")) { fb_queue_erase("system"); } fb_queue_flash("system", data, sz); }
void do_update(usb_handle *usb, char *fn, int erase_first) { void *zdata; unsigned zsize; void *data; unsigned sz; zipfile_t zip; int fd; int rc; struct fastboot_buffer buf; size_t i; queue_info_dump(); fb_queue_query_save("product", cur_product, sizeof(cur_product)); zdata = load_file(fn, &zsize); if (zdata == 0) die("failed to load '%s': %s", fn, strerror(errno)); zip = init_zipfile(zdata, zsize); if(zip == 0) die("failed to access zipdata in '%s'"); data = unzip_file(zip, "android-info.txt", &sz); if (data == 0) { char *tmp; /* fallback for older zipfiles */ data = unzip_file(zip, "android-product.txt", &sz); if ((data == 0) || (sz < 1)) { die("update package has no android-info.txt or android-product.txt"); } tmp = malloc(sz + 128); if (tmp == 0) die("out of memory"); sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data); data = tmp; sz = strlen(tmp); } setup_requirements(data, sz); for (i = 0; i < ARRAY_SIZE(images); i++) { fd = unzip_to_file(zip, images[i].img_name); if (fd < 0) { if (images[i].is_optional) continue; die("update package missing %s", images[i].img_name); } rc = load_buf_fd(usb, fd, &buf); if (rc) die("cannot load %s from flash", images[i].img_name); do_update_signature(zip, images[i].sig_name); if (erase_first && needs_erase(images[i].part_name)) { fb_queue_erase(images[i].part_name); } flash_buf(images[i].part_name, &buf); /* not closing the fd here since the sparse code keeps the fd around * but hasn't mmaped data yet. The tmpfile will get cleaned up when the * program exits. */ } }
int main(int argc, char** argv) { FILE* f; size_t size, unsize; void* buf; void* scratch; zipfile_t zip; zipentry_t entry; int err; enum { HUH, LIST, UNZIP } what = HUH; if (strcmp(argv[2], "-l") == 0 && argc == 3) { what = LIST; } else if (strcmp(argv[2], "-u") == 0 && argc == 5) { what = UNZIP; } else { fprintf(stderr, "usage: test_zipfile ZIPFILE -l\n" " lists the files in the zipfile\n" " test_zipfile ZIPFILE -u FILENAME SAVETO\n" " saves FILENAME from the zip file into SAVETO\n"); return 1; } f = fopen(argv[1], "r"); if (f == NULL) { fprintf(stderr, "couldn't open %s\n", argv[1]); return 1; } fseek(f, 0, SEEK_END); size = ftell(f); rewind(f); buf = malloc(size); fread(buf, 1, size, f); zip = init_zipfile(buf, size); if (zip == NULL) { fprintf(stderr, "inti_zipfile failed\n"); return 1; } fclose(f); switch (what) { case HUH: break; case LIST: dump_zipfile(stdout, zip); break; case UNZIP: entry = lookup_zipentry(zip, argv[3]); if (entry == NULL) { fprintf(stderr, "zip file '%s' does not contain file '%s'\n", argv[1], argv[1]); return 1; } f = fopen(argv[4], "w"); if (f == NULL) { fprintf(stderr, "can't open file for writing '%s'\n", argv[4]); return 1; } unsize = get_zipentry_size(entry); size = unsize * 1.001; scratch = malloc(size); printf("scratch=%p\n", scratch); err = decompress_zipentry(entry, scratch, size); if (err != 0) { fprintf(stderr, "error decompressing file\n"); return 1; } fwrite(scratch, unsize, 1, f); free(scratch); fclose(f); break; } free(buf); return 0; }
void do_flashall(char *fn) { void *zdata; unsigned zsize; void *data; unsigned sz; zipfile_t zip; struct config conf; char ver[FB_RESPONSE_SZ + 1]; char *pc; int status; /* get target IFWI major version */ fprintf(stderr, "query system info...\n"); fb_queue_query_save("ifwi", ver, sizeof(ver)); usb = open_device(); fb_execute_queue(usb); if ((pc = strchr(ver, '.'))) *pc = 0; queue_info_dump(); zdata = load_file(fn, &zsize); if (zdata == 0) die("failed to load '%s': %s", fn, strerror(errno)); zip = init_zipfile(zdata, zsize); if(zip == 0) die("failed to access zipdata in '%s', is it a zip file?", fn); /* is converted-system tarball? */ data = unzip_file(zip, CONFIG_FILE, &sz); if (data == 0) { /* is raw container tarball? */ data = unzip_file(zip, FW_CONFIG, &sz); if (data != 0 && parse_config(data, (size_t)sz, &conf, ver)) die("parse config: %s failed.", FW_CONFIG); data = unzip_file(zip, PREOS_CONFIG, &sz); if (data != 0 && parse_config(data, (size_t)sz, &conf, ver)) die("parse config: %s failed.", PREOS_CONFIG); } else { if (parse_config(data, (size_t)sz, &conf, ver)) die("parse config failed."); } /* * every component is optional, no need to check * the second of unzip_file argument if it's not null * memcmp can handle NULL pointer. */ data = unzip_file(zip, conf.fwr_dnx, &sz); if (data != 0) fb_queue_stream_flash("dnx", data, sz); data = unzip_file(zip, conf.ifwi, &sz); if (data != 0) fb_queue_stream_flash("ifwi", data, sz); data = unzip_file(zip, conf.boot, &sz); if (data != 0) fb_queue_stream_flash("boot", data, sz); data = unzip_file(zip, conf.preos, &sz); if (data != 0) fb_queue_stream_flash("preos", data, sz); /* try to get platform image. * first, try gziped. * second, try bzip2. * at last, try raw image */ data = unzip_file(zip, PLATFORM_IMG ".gz", &sz); if (data == 0) { data = unzip_file(zip, PLATFORM_IMG ".bz2", &sz); if (data == 0) { data = unzip_file(zip, PLATFORM_IMG, &sz); } } if (data != 0) fb_queue_stream_flash("platform", data, sz); /* data and csa partition image */ data = unzip_file(zip, DATA_IMG ".gz", &sz); if (data == 0) { data = unzip_file(zip, DATA_IMG ".bz2", &sz); if (data == 0) { data = unzip_file(zip, DATA_IMG, &sz); } } if (data != 0) fb_queue_stream_flash("data", data, sz); data = unzip_file(zip, CSA_IMG ".gz", &sz); if (data == 0) { data = unzip_file(zip, CSA_IMG ".bz2", &sz); if (data == 0) { data = unzip_file(zip, CSA_IMG, &sz); } } if (data != 0) fb_queue_stream_flash("csa", data, sz); }
static int sync_send(int fd, const char *lpath, const char *rpath, unsigned mtime, mode_t mode, int verifyApk) { syncmsg msg; int len, r; syncsendbuf *sbuf = &send_buffer; char* file_buffer = NULL; int size = 0; char tmp[64]; len = strlen(rpath); if(len > 1024) goto fail; snprintf(tmp, sizeof(tmp), ",%d", mode); r = strlen(tmp); if (verifyApk) { int lfd; zipfile_t zip; zipentry_t entry; int amt; // if we are transferring an APK file, then sanity check to make sure // we have a real zip file that contains an AndroidManifest.xml // this requires that we read the entire file into memory. lfd = adb_open(lpath, O_RDONLY); if(lfd < 0) { fprintf(stderr,"cannot open '%s': %s\n", lpath, strerror(errno)); return -1; } size = adb_lseek(lfd, 0, SEEK_END); if (size == -1 || -1 == adb_lseek(lfd, 0, SEEK_SET)) { fprintf(stderr, "error seeking in file '%s'\n", lpath); adb_close(lfd); return 1; } file_buffer = (char *)malloc(size); if (file_buffer == NULL) { fprintf(stderr, "could not allocate buffer for '%s'\n", lpath); adb_close(lfd); return 1; } amt = adb_read(lfd, file_buffer, size); if (amt != size) { fprintf(stderr, "error reading from file: '%s'\n", lpath); adb_close(lfd); free(file_buffer); return 1; } adb_close(lfd); zip = init_zipfile(file_buffer, size); if (zip == NULL) { fprintf(stderr, "file '%s' is not a valid zip file\n", lpath); free(file_buffer); return 1; } entry = lookup_zipentry(zip, "AndroidManifest.xml"); release_zipfile(zip); if (entry == NULL) { fprintf(stderr, "file '%s' does not contain AndroidManifest.xml\n", lpath); free(file_buffer); return 1; } } msg.req.id = ID_SEND; msg.req.namelen = htoll(len + r); if(writex(fd, &msg.req, sizeof(msg.req)) || writex(fd, rpath, len) || writex(fd, tmp, r)) { free(file_buffer); goto fail; } if (file_buffer) { write_data_buffer(fd, file_buffer, size, sbuf); free(file_buffer); } else if (S_ISREG(mode)) write_data_file(fd, lpath, sbuf); #ifdef HAVE_SYMLINKS else if (S_ISLNK(mode)) write_data_link(fd, lpath, sbuf); #endif else goto fail; msg.data.id = ID_DONE; msg.data.size = htoll(mtime); if(writex(fd, &msg.data, sizeof(msg.data))) goto fail; if(readx(fd, &msg.status, sizeof(msg.status))) return -1; if(msg.status.id != ID_OKAY) { if(msg.status.id == ID_FAIL) { len = ltohl(msg.status.msglen); if(len > 256) len = 256; if(readx(fd, sbuf->data, len)) { return -1; } sbuf->data[len] = 0; } else strcpy(sbuf->data, "unknown reason"); fprintf(stderr,"failed to copy '%s' to '%s': %s\n", lpath, rpath, sbuf->data); return -1; } return 0; fail: fprintf(stderr,"protocol failure\n"); adb_close(fd); return -1; }