#include #include #include #include #include #include #include #include struct entry { const char *cmd; int (* main)(int argc, char *argv[]); void (* show_usage)(const char *prog_name, const char *cmd); char *desc; }; struct entry entries[] = { { "get-offset", do_get_offset, show_get_offset_usage, "print the offset of the payload within an image" }, { "get-size", do_get_size, show_get_size_usage, "print the size of the payload within an image" }, { "pack", do_pack, show_pack_usage, "pack the payload within an image" }, { "sign", do_sign, show_sign_usage, "sign an image" }, { "unpack", do_unpack, show_unpack_usage, "unpack the payload of an image" }, { "verify", do_verify, show_verify_usage, "verify the signatures of an image" }, { NULL, NULL, NULL, NULL }, }; static void show_usage(const char *prog_name) { struct entry *entry; fprintf(stderr, "usage: %s ...\n\n" "ROTS utilities to work with signed images\n\n" "Available commands:\n", prog_name); for (entry = entries; entry->cmd; ++entry) { fprintf(stderr, "\t%-16s %s\n", entry->cmd, entry->desc); } } static void show_cmd_usage(char *prog_name, char *cmd) { struct entry *entry; for (entry = entries; entry->cmd; ++entry) { if (strcmp(entry->cmd, cmd) != 0) continue; if (!entry->show_usage) break; entry->show_usage(prog_name, cmd); return; } show_usage(prog_name); } int main(int argc, char *argv[]) { struct entry *entry; if (argc < 2) { show_usage(argv[0]); return -1; } if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) { if (argc < 3) show_usage(argv[0]); else show_cmd_usage(argv[0], argv[2]); return -1; } for (entry = entries; entry->cmd; ++entry) { if (strcmp(entry->cmd, argv[1]) == 0) return entry->main(argc, argv); } show_usage(argv[0]); return -1; }