#include #include #include #include #include #include #include #include #include #include enum { OPTION_HELP = 'h', OPTION_IMAGE = 'i', }; struct args { const char *image; }; static struct opt_desc opt_descs[] = { { "-i", "--image=PATH", "the image in ROTS format" }, { "-h", "--help", "display this help and exit" }, { NULL, NULL, NULL }, }; static int parse_args(struct args *args, int argc, char *argv[]) { struct option options[] = { { "help", no_argument, NULL, OPTION_HELP }, { "image", required_argument, 0, OPTION_IMAGE }, { NULL, 0, 0, 0 }, }; int ret; while ((ret = getopt_long(argc, (char * const *)argv, "hi:d:k:", options, NULL)) >= 0) { switch (ret) { case OPTION_HELP: return -1; case OPTION_IMAGE: args->image = optarg; break; default: break; } } return 0; } void show_get_offset_usage(const char *prog_name, const char *cmd) { fprintf(stderr, "usage: %s %s [option]...\n\n" "print the offset of the payload within an image\n\n", prog_name, cmd); format_options(opt_descs); } void show_get_size_usage(const char *prog_name, const char *cmd) { fprintf(stderr, "usage: %s %s [option]...\n\n" "print the size of the payload within an image\n\n", prog_name, cmd); format_options(opt_descs); } int do_get_offset(int argc, char *argv[]) { struct args args; struct rots_hdr hdr; FILE *fp; if (parse_args(&args, argc, argv) < 0) { show_get_offset_usage(argv[0], argv[1]); return -1; } if (!(fp = fopen(args.image, "rb"))) { fprintf(stderr, "error: file '%s' not found.\n", args.image); return -1; } if (rots_read_hdr(fp, &hdr) < 0) { fprintf(stderr, "error: file '%s' is not a ROTS-image.\n", args.image); goto err_close_fp; } printf("%zu\n", ftell(fp)); fclose(fp); return 0; err_close_fp: fclose(fp); return -1; } int do_get_size(int argc, char *argv[]) { struct args args; struct rots_hdr hdr; FILE *fp; if (parse_args(&args, argc, argv) < 0) { show_get_size_usage(argv[0], argv[1]); return -1; } if (!(fp = fopen(args.image, "rb"))) { fprintf(stderr, "error: file '%s' not found.\n", args.image); return -1; } if (rots_read_hdr(fp, &hdr) < 0) { fprintf(stderr, "error: file '%s' is not a ROTS-image.\n", args.image); goto err_close_fp; } printf("%zu\n", hdr.size); fclose(fp); return 0; err_close_fp: fclose(fp); return -1; }