void Pack_Image(Image *image) { pack_image(image); }
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 bladerf_image_write(struct bladerf_image *img, const char *file) { int rv; FILE *f = NULL; uint8_t *buf = NULL; size_t buf_len; /* Ensure the format identifier is correct */ if (memcmp(img->magic, image_magic, BLADERF_IMAGE_MAGIC_LEN) != 0) { #ifdef LOGGING_ENABLED char badmagic[BLADERF_IMAGE_MAGIC_LEN + 1]; memset(badmagic, 0, sizeof(badmagic)); memcpy(&badmagic, &img->magic, BLADERF_IMAGE_MAGIC_LEN); log_debug("Invalid file format magic value: %s\n", badmagic); #endif return BLADERF_ERR_INVAL; } /* Check for a valid image type */ if (!image_type_is_valid(img->type)) { log_debug("Invalid image type: %d\n", img->type); return BLADERF_ERR_INVAL; } /* Just to be tiny bit paranoid... */ if (!img->data) { log_debug("Image data pointer is NULL\n"); return BLADERF_ERR_INVAL; } buf_len = CALC_IMAGE_SIZE(img->length); buf = (uint8_t *)calloc(1, buf_len); if (!buf) { log_verbose("calloc failed: %s\n", strerror(errno)); return BLADERF_ERR_MEM; } /* If the type is RAW, we should only allow erase-block aligned * addresses and lengths */ if (img->type == BLADERF_IMAGE_TYPE_RAW) { if (img->address % BLADERF_FLASH_EB_SIZE != 0) { log_debug("Image address must be erase block-aligned for RAW.\n"); rv = BLADERF_ERR_INVAL; goto error; } else if (img->length % BLADERF_FLASH_EB_SIZE != 0) { log_debug("Image length must be erase block-aligned for RAW.\n"); rv = BLADERF_ERR_INVAL; goto error; } } pack_image(img, buf); f = fopen(file, "wb"); if (!f) { log_debug("Failed to open \"%s\": %s\n", file, strerror(errno)); rv = BLADERF_ERR_IO; goto error; } rv = file_write(f, buf, buf_len); error: if (f) { fclose(f); } free(buf); return rv; }