static void cmd_show_options_print(struct cmd *self, struct cmdq_item *item, struct options_entry *o, int idx) { struct options_array_item *a; const char *name = options_name(o); char *value, *tmp = NULL, *escaped; if (idx != -1) { xasprintf(&tmp, "%s[%d]", name, idx); name = tmp; } else { if (options_isarray(o)) { a = options_array_first(o); if (a == NULL) { if (!args_has(self->args, 'v')) cmdq_print(item, "%s", name); return; } while (a != NULL) { idx = options_array_item_index(a); cmd_show_options_print(self, item, o, idx); a = options_array_next(a); } return; } } value = options_tostring(o, idx, 0); if (args_has(self->args, 'v')) cmdq_print(item, "%s", value); else if (options_isstring(o)) { utf8_stravis(&escaped, value, VIS_OCTAL|VIS_TAB|VIS_NL|VIS_DQ); cmdq_print(item, "%s \"%s\"", name, escaped); free(escaped); } else cmdq_print(item, "%s %s", name, value); free(value); free(tmp); }
static enum cmd_retval cmd_show_options_all(struct cmd *self, struct cmdq_item *item, struct options *oo) { struct options_entry *o; struct options_array_item *a; u_int idx; const struct options_table_entry *oe; o = options_first(oo); while (o != NULL) { oe = options_table_entry(o); if ((self->entry != &cmd_show_hooks_entry && !args_has(self->args, 'H') && oe != NULL && (oe->flags & OPTIONS_TABLE_IS_HOOK)) || (self->entry == &cmd_show_hooks_entry && (oe == NULL || (~oe->flags & OPTIONS_TABLE_IS_HOOK)))) { o = options_next(o); continue; } if (!options_isarray(o)) cmd_show_options_print(self, item, o, -1); else if ((a = options_array_first(o)) == NULL) { if (!args_has(self->args, 'v')) cmdq_print(item, "%s", options_name(o)); } else { while (a != NULL) { idx = options_array_item_index(a); cmd_show_options_print(self, item, o, idx); a = options_array_next(a); } } o = options_next(o); } return (CMD_RETURN_NORMAL); }
int process_arguments(int argc, char* argv[]) { //-------------------------------------------------------------------- // Parse input variables. //-------------------------------------------------------------------- GetPot cl(argc, argv); if (cl.size() == 1 || cl.search(2, "-h", "--help")) { print_help(); return 1; } std::string updatername; if (cl.search("-u") || cl.search("--updater")) updatername = cl.next(""); if (updatername.empty()) { std::cerr << "Updater executable must be specified." << std::endl; return 2; } std::string firmarename = default_firmware_name(updatername); if (cl.search("-f") || cl.search("--firmware")) firmarename = cl.next(firmarename.c_str()); bool verbose = false; if (cl.search("-V") || cl.search("--verbose")) verbose = true; // Get or find the firmware archive key. std::string key; if (cl.search("-k") || cl.search("--key")) key = cl.next(""); if (key.empty()) { if (verbose) std::cout << "[*] Looking for firmware archive key..." << std::endl; shared::bytes buffer; if (!shared::read_file(updatername, buffer)) { std::cerr << "Failed to read the firmware updater executable." << std::endl; return 3; } key = zen::find_firmware_key(&buffer[0], buffer.size()); if (key.empty()) { std::cerr << "Failed to find the firmware archive key." << std::endl; return 4; } } // Get or find the firmware archive offset. std::string offset; dword offset_pa = 0; if (cl.search("-o") || cl.search("--ofset")) offset = cl.next(""); if (offset.empty()) { if (verbose) std::cout << "[*] Looking for firmware archive offset..." << std::endl; dword offset_va = 0; if (!zen::find_firmware_archive(updatername, offset_va, offset_pa)) { std::cerr << "Failed to find the firmware archive offset." << std::endl; return 5; } } else { int offset_val; if (!sscanf(offset.c_str(), "0x%x", &offset_val)) { if (!sscanf(offset.c_str(), "0x%X", &offset_val)) { std::cerr << "\'" << offset << "\' is not a valid c-style hexadecimal value." << std::endl; return 6; } } offset_pa = static_cast<dword>(offset_val); } // Read firmware archive size. shared::bytes buffer; if (!shared::read_file(updatername, buffer, offset_pa, sizeof(dword))) { std::cerr << "Failed to read the firmware archive size." << std::endl; return 7; } dword archive_size = *(dword*)&buffer[0]; if (verbose) { std::cout << "[*] Printing input variables..." << std::endl; std::cout << " Updater executable: " << updatername << std::endl; std::cout << " Firmware archive: " << firmarename << std::endl; std::cout << " Key: " << key << std::endl; std::cout << " Offset: " << std::hex << std::showbase << std::setw(10) << std::setfill('0') << std::internal << offset_pa << std::endl; std::cout << " Size: " << std::hex << std::showbase << std::setw(10) << std::setfill('0') << std::internal << archive_size << std::endl; } //-------------------------------------------------------------------- // Extract the firmware archive from the updater. //-------------------------------------------------------------------- if (verbose) std::cout << "[*] Reading firmware archive..." << std::endl; // Read the firmware archive. offset_pa += sizeof(dword); if (!shared::read_file(updatername, buffer, offset_pa, archive_size)) { std::cerr << "Failed to read the firmware archive." << std::endl; return 8; } if (verbose) std::cout << "[*] Decrypting firmware archive..." << std::endl; // Decrypt the firmware archive. if (!zen::crypt_firmware(key.c_str(), &buffer[0], buffer.size())) { std::cerr << "Failed to decrypt the firmware archive." << std::endl; return 9; } if (verbose) std::cout << "[*] Decompressing firmware archive..." << std::endl; // Inflate the firmware archive to the output file. if (!shared::inflate_to_file(buffer, firmarename.c_str())) { std::cerr << "Failed to decompress the firmware archive." << std::endl; return 10; } //-------------------------------------------------------------------- // Generate an options file for the extracted firmware archive. //-------------------------------------------------------------------- // Get options filename for the given input file. std::string optionsname = options_name(updatername); if (verbose) std::cout << "[*] Producing options file..." << std::endl; // Produce options file for the given input file. std::ofstream ofs; ofs.open(optionsname.c_str(), std::ios::binary); if (!ofs) { std::cerr << "Failed to create firmware archive options file." << std::endl; return 11; } time_t timeval = time(NULL); ofs << "# Options file generated at: " << ctime(&timeval) << "updater = \'" << shared::double_quote(updatername) << "\'" << std::endl << "firmware = \'" << shared::double_quote(firmarename) << "\'" << std::endl << "offset = " << (offset_pa - sizeof(dword)) << std::endl << "size = " << archive_size << std::endl << "key = \'" << key << "\'" << std::endl; return 0; }