int bladerf_image_read(struct bladerf_image *img, const char *file) { int rv = -1; uint8_t *buf = NULL; size_t buf_len; rv = file_read_buffer(file, &buf, &buf_len); if (rv < 0) { goto bladerf_image_read_out; } rv = verify_checksum(buf, buf_len); if (rv < 0) { goto bladerf_image_read_out; } /* Note: On success, buf->data = buf, with the data memmove'd over. * Static analysis tools might indicate a false postive leak when * buf goes out of scope with rv == 0 */ rv = unpack_image(img, buf, buf_len); bladerf_image_read_out: if (rv != 0) { free(buf); } return rv; }
int main(int argc, char **argv) { struct stat statbuf; char outfn[512]; char *out, *in; int c, rc; /* Generate program name from argv[0] */ progname = argv[0]; if (progname[0] == '.' && progname[1] == '/') progname += 2; /* Setup default output format */ flag_compat_output = OUTPUT_UNIMG; /* Now scan the cmdline options */ do { c = getopt(argc, argv, "vurh"); if (c == EOF) break; switch (c) { case 'u': flag_compat_output = OUTPUT_UNIMG; break; case 'r': flag_compat_output = OUTPUT_IMGREPACKER; break; case 'v': flag_verbose++; break; case 'h': fprintf(stderr, "%s [-vurh] {image dir|dir image}\n" " -r imgRepacker compatibility\n" " -u unimg.exe compatibility\n" " -v Be verbose\n" " -h Print help\n", argv[0]); return -1; case '?': fprintf(stderr, "%s: invalid option -%c\n", argv[0], optopt); return 1; } } while (1); if (argc - optind > 2) { fprintf(stderr, "%s: extra arguments\n", argv[0]); return 1; } else if (argc - optind < 1) { fprintf(stderr, "%s: missing file argument\n", argv[0]); return 1; } /* If we get here, we have a file spec and possibly options */ crypto_init(); rc = stat(argv[optind], &statbuf); if (rc) { fprintf(stderr, "%s: cannot stat '%s'!\n", argv[0], argv[optind]); return 1; } out = (argc - optind) == 2 ? argv[optind+1] : NULL; in = argv[optind]; if (S_ISDIR(statbuf.st_mode)) { /* We're packing, lets see if we have to generate the output image filename ourselfs */ if (out == NULL) { int len = strlen(in); strcpy(outfn, in); if (in[len-1] == '/') len--; printf("%s\n", in+len-5); if (len > 5 && strncmp(in+len-5, ".dump", 5) == 0) outfn[len-5] = '\0'; else { outfn[len] = '\0'; strcat(outfn, ".img"); } out = outfn; } fprintf(stderr, "%s: packing %s into %s\n", argv[0], in, out); pack_image(in, out); } else { /* We're unpacking, lets see if we have to generate the output directory name ourself */ if (out == NULL) { strcpy(outfn, in); strcat(outfn, ".dump"); } else { strcpy(outfn, out); } out = outfn; fprintf(stderr, "%s: unpacking %s to %s\n", argv[0], in, out); unpack_image(in, out); } return 0; }
int unpack(int szb_path,szb_header_t *szb_header,image_node *header) { unsigned int i; image_node *prefix=header; for (i = 0; i <szb_header->imagecount; ++i) { if (memcmp(prefix->data->partname,"bootloader",10)==0) { printf("start unpack uboot.bin......\n"); crc_checksum(szb_path,prefix,"uboot.img"); unpack_image(szb_path,prefix,"uboot.bin"); prefix=prefix->next; continue; } if (memcmp(prefix->data->partname,"boot",4)==0) { printf("start unpack boot.img......\n"); crc_checksum(szb_path,prefix,"boot.img"); unpack_image(szb_path,prefix,"boot.img"); prefix=prefix->next; continue; } if (memcmp(prefix->data->partname,"recovery",8)==0) { printf("start unpack recovery.img......\n"); crc_checksum(szb_path,prefix,"recovery.img"); unpack_image(szb_path,prefix,"recovery.img"); prefix=prefix->next; continue; } if (memcmp(prefix->data->partname,"system",6)==0) { printf("start unpack system.img\n"); crc_checksum(szb_path,prefix,"system.img"); unpack_image(szb_path,prefix,"system.img"); prefix=prefix->next; continue; } if (memcmp(prefix->data->partname,"cpimage",7)==0) { printf("start unpack cpimage.img ......\n"); crc_checksum(szb_path,prefix,"cpimage.img"); unpack_image(szb_path,prefix,"cpimage.img"); prefix=prefix->next; continue; } if (memcmp(prefix->data->partname,"preload",7)==0) { printf("start unpack preload.img......\n"); crc_checksum(szb_path,prefix,"preload.img"); unpack_image(szb_path,prefix,"preload.img"); prefix=prefix->next; continue; } if (memcmp(prefix->data->partname,"userdata",8)==0) { printf("start analysis userdata.img......\n"); unpack_image(szb_path,prefix,"userdata.img"); prefix=prefix->next; continue; } if (memcmp(prefix->data->partname,"cache",5)==0) { printf("start analysis cache.img...... \n"); unpack_image(szb_path,prefix,"cache.img"); prefix=prefix->next; continue; } } return 0; }