bool HWStubDevice::Probe() { if(!Open()) return false; // get target int ret = hwstub_get_desc(m_hwdev, HWSTUB_DT_TARGET, &m_hwdev_target, sizeof(m_hwdev_target)); if(ret != sizeof(m_hwdev_target)) goto Lerr; // get STMP information if(m_hwdev_target.dID == HWSTUB_TARGET_STMP) { ret = hwstub_get_desc(m_hwdev, HWSTUB_DT_STMP, &m_hwdev_stmp, sizeof(m_hwdev_stmp)); if(ret != sizeof(m_hwdev_stmp)) goto Lerr; } else if(m_hwdev_target.dID == HWSTUB_TARGET_PP) { ret = hwstub_get_desc(m_hwdev, HWSTUB_DT_PP, &m_hwdev_pp, sizeof(m_hwdev_pp)); if(ret != sizeof(m_hwdev_pp)) goto Lerr; } Close(); return true; Lerr: Close(); return false; }
struct hwstub_device_t *hwstub_open(libusb_device_handle *handle) { struct hwstub_device_t *dev = malloc(sizeof(struct hwstub_device_t)); memset(dev, 0, sizeof(struct hwstub_device_t)); dev->handle = handle; dev->intf = -1; dev->buf_sz = 1024; /* default size */ libusb_device *mydev = libusb_get_device(dev->handle); dev->intf = hwstub_probe(mydev); if(dev->intf == -1) goto Lerr; /* try to get version */ struct hwstub_version_desc_t m_hwdev_ver; int sz = hwstub_get_desc(dev, HWSTUB_DT_VERSION, &m_hwdev_ver, sizeof(m_hwdev_ver)); if(sz != sizeof(m_hwdev_ver)) goto Lerr; /* major version must match, minor version is taken to be the minimum between * what library and device support */ if(m_hwdev_ver.bMajor != HWSTUB_VERSION_MAJOR) goto Lerr; dev->minor_ver = MIN(m_hwdev_ver.bMinor, HWSTUB_VERSION_MINOR); /* try to get actual buffer size */ struct hwstub_layout_desc_t layout; sz = hwstub_get_desc(dev, HWSTUB_DT_LAYOUT, &layout, sizeof(layout)); if(sz == (int)sizeof(layout)) dev->buf_sz = layout.dBufferSize; return dev; Lerr: free(dev); return NULL; }
bool HWStubDevice::Probe() { struct libusb_device_descriptor desc; if(libusb_get_device_descriptor(m_dev, &desc)) return false; if(desc.idVendor != HWSTUB_USB_VID || desc.idProduct != HWSTUB_USB_PID) return false; if(!Open()) return false; int ret = hwstub_get_desc(m_hwdev, HWSTUB_DT_VERSION, &m_hwdev_ver, sizeof(m_hwdev_ver)); if(ret != sizeof(m_hwdev_ver)) goto Lerr; if(m_hwdev_ver.bMajor != HWSTUB_VERSION_MAJOR || m_hwdev_ver.bMinor < HWSTUB_VERSION_MINOR) goto Lerr; // get target ret = hwstub_get_desc(m_hwdev, HWSTUB_DT_TARGET, &m_hwdev_target, sizeof(m_hwdev_target)); if(ret != sizeof(m_hwdev_target)) goto Lerr; // get STMP information if(m_hwdev_target.dID == HWSTUB_TARGET_STMP) { ret = hwstub_get_desc(m_hwdev, HWSTUB_DT_STMP, &m_hwdev_stmp, sizeof(m_hwdev_stmp)); if(ret != sizeof(m_hwdev_stmp)) goto Lerr; } Close(); return true; Lerr: Close(); return false; }
int main(int argc, char **argv) { bool quiet = false; struct hwstub_device_t *hwdev; enum image_type_t type = IT_DETECT; // parse command line while(1) { static struct option long_options[] = { {"help", no_argument, 0, '?'}, {"quiet", no_argument, 0, 'q'}, {"type", required_argument, 0, 't'}, {0, 0, 0, 0} }; int c = getopt_long(argc, argv, "?qt:", long_options, NULL); if(c == -1) break; switch(c) { case -1: break; case 'q': quiet = true; break; case '?': usage(); break; case 't': if(strcmp(optarg, "raw") == 0) type = IT_RAW; else if(strcmp(optarg, "rockbox") == 0) type = IT_ROCKBOX; else if(strcmp(optarg, "detect") == 0) type = IT_DETECT; else { fprintf(stderr, "Unknown file type '%s'\n", optarg); return 1; } break; default: abort(); } } if(optind + 2 != argc) usage(); char *end; unsigned long addr = strtoul(argv[optind], &end, 0); if(*end) { fprintf(stderr, "Invalid load address\n"); return 2; } FILE *f = fopen(argv[optind + 1], "rb"); if(f == NULL) { fprintf(stderr, "Cannot open file for reading: %m\n"); return 3; } fseek(f, 0, SEEK_END); size_t size = ftell(f); fseek(f, 0, SEEK_SET); unsigned char *buffer = (unsigned char*)malloc(size); fread(buffer, size, 1, f); fclose(f); if(type == IT_ROCKBOX || type == IT_DETECT) { enum image_type_t det = detect_type(buffer, size); if(type == IT_ROCKBOX && det != IT_ROCKBOX) { if(!could_be_rockbox(buffer, size)) fprintf(stderr, "This file does not appear to be valid rockbox image.\n"); return 4; } if(type == IT_DETECT && det == IT_RAW) could_be_rockbox(buffer, size); type = det; if(type == IT_ROCKBOX) { if(!quiet) printf("Rockox image is for player %s (%.4s)\n", get_player_name(buffer + 4), buffer + 4); memmove(buffer, buffer + 8, size - 8); size -= 8; } } if(!quiet) { if(type == IT_RAW) printf("Loading raw image at %#lx\n", addr); else printf("Loading rockbox image at %#lx\n", addr); } // create usb context libusb_context *ctx; libusb_init(&ctx); libusb_set_debug(ctx, 3); // look for device if(!quiet) printf("Looking for device %#04x:%#04x...\n", HWSTUB_USB_VID, HWSTUB_USB_PID); libusb_device_handle *handle = libusb_open_device_with_vid_pid(ctx, HWSTUB_USB_VID, HWSTUB_USB_PID); if(handle == NULL) { fprintf(stderr, "No device found\n"); return 1; } // admin stuff libusb_device *mydev = libusb_get_device(handle); if(!quiet) { printf("device found at %d:%d\n", libusb_get_bus_number(mydev), libusb_get_device_address(mydev)); } hwdev = hwstub_open(handle); if(hwdev == NULL) { fprintf(stderr, "Cannot probe device!\n"); return 1; } // get hwstub information struct hwstub_version_desc_t hwdev_ver; int ret = hwstub_get_desc(hwdev, HWSTUB_DT_VERSION, &hwdev_ver, sizeof(hwdev_ver)); if(ret != sizeof(hwdev_ver)) { fprintf(stderr, "Cannot get version!\n"); goto Lerr; } if(hwdev_ver.bMajor != HWSTUB_VERSION_MAJOR || hwdev_ver.bMinor < HWSTUB_VERSION_MINOR) { printf("Warning: this tool is possibly incompatible with your device:\n"); printf("Device version: %d.%d.%d\n", hwdev_ver.bMajor, hwdev_ver.bMinor, hwdev_ver.bRevision); printf("Host version: %d.%d\n", HWSTUB_VERSION_MAJOR, HWSTUB_VERSION_MINOR); } ret = hwstub_rw_mem(hwdev, 0, addr, buffer, size); if(ret != (int)size) { fprintf(stderr, "Image write failed: %d\n", ret); goto Lerr; } hwstub_jump(hwdev, addr); hwstub_release(hwdev); return 0; Lerr: // display log if handled fprintf(stderr, "Device log:\n"); do { char buffer[128]; int length = hwstub_get_log(hwdev, buffer, sizeof(buffer) - 1); if(length <= 0) break; buffer[length] = 0; fprintf(stderr, "%s", buffer); }while(1); hwstub_release(hwdev); return 1; }
int main(int argc, char **argv) { const char *lua_init = "init.lua"; std::vector< std::pair< exec_type, std::string > > startup_cmds; // parse command line while(1) { static struct option long_options[] = { {"help", no_argument, 0, '?'}, {"quiet", no_argument, 0, 'q'}, {0, 0, 0, 0} }; int c = getopt_long(argc, argv, "?qi:e:f:", long_options, NULL); if(c == -1) break; switch(c) { case -1: break; case 'q': g_quiet = true; break; case '?': usage(); break; case 'i': lua_init = optarg; break; case 'e': startup_cmds.push_back(std::make_pair(exec_cmd, std::string(optarg))); break; case 'f': startup_cmds.push_back(std::make_pair(exec_file, std::string(optarg))); break; default: abort(); } } // load register descriptions std::vector< soc_t > socs; for(int i = optind; i < argc; i++) { socs.push_back(soc_t()); if(!soc_desc_parse_xml(argv[i], socs[socs.size() - 1])) { printf("Cannot load description '%s'\n", argv[i]); return 2; } } // create usb context libusb_context *ctx; libusb_init(&ctx); libusb_set_debug(ctx, 3); // look for device if(!g_quiet) printf("Looking for hwstub device ...\n"); // open first device libusb_device **list; ssize_t cnt = hwstub_get_device_list(ctx, &list); if(cnt <= 0) { printf("No device found\n"); return 1; } libusb_device_handle *handle; if(libusb_open(list[0], &handle) != 0) { printf("Cannot open device\n"); return 1; } libusb_free_device_list(list, 1); // admin stuff libusb_device *mydev = libusb_get_device(handle); if(!g_quiet) { printf("device found at %d:%d\n", libusb_get_bus_number(mydev), libusb_get_device_address(mydev)); } g_hwdev = hwstub_open(handle); if(g_hwdev == NULL) { printf("Cannot open device!\n"); return 1; } // get hwstub information int ret = hwstub_get_desc(g_hwdev, HWSTUB_DT_VERSION, &g_hwdev_ver, sizeof(g_hwdev_ver)); if(ret != sizeof(g_hwdev_ver)) { printf("Cannot get version!\n"); goto Lerr; } if(g_hwdev_ver.bMajor != HWSTUB_VERSION_MAJOR || g_hwdev_ver.bMinor < HWSTUB_VERSION_MINOR) { printf("Warning: this tool is possibly incompatible with your device:\n"); printf("Device version: %d.%d.%d\n", g_hwdev_ver.bMajor, g_hwdev_ver.bMinor, g_hwdev_ver.bRevision); printf("Host version: %d.%d\n", HWSTUB_VERSION_MAJOR, HWSTUB_VERSION_MINOR); } // get memory layout information ret = hwstub_get_desc(g_hwdev, HWSTUB_DT_LAYOUT, &g_hwdev_layout, sizeof(g_hwdev_layout)); if(ret != sizeof(g_hwdev_layout)) { printf("Cannot get layout: %d\n", ret); goto Lerr; } // get target ret = hwstub_get_desc(g_hwdev, HWSTUB_DT_TARGET, &g_hwdev_target, sizeof(g_hwdev_target)); if(ret != sizeof(g_hwdev_target)) { printf("Cannot get target: %d\n", ret); goto Lerr; } // get STMP specific information if(g_hwdev_target.dID == HWSTUB_TARGET_STMP) { ret = hwstub_get_desc(g_hwdev, HWSTUB_DT_STMP, &g_hwdev_stmp, sizeof(g_hwdev_stmp)); if(ret != sizeof(g_hwdev_stmp)) { printf("Cannot get stmp: %d\n", ret); goto Lerr; } } // get PP specific information if(g_hwdev_target.dID == HWSTUB_TARGET_PP) { ret = hwstub_get_desc(g_hwdev, HWSTUB_DT_PP, &g_hwdev_pp, sizeof(g_hwdev_pp)); if(ret != sizeof(g_hwdev_pp)) { printf("Cannot get pp: %d\n", ret); goto Lerr; } } /** Init lua */ // create lua state g_lua = luaL_newstate(); if(g_lua == NULL) { printf("Cannot create lua state\n"); return 1; } // import hwstub if(!my_lua_import_hwstub()) printf("Cannot import hwstub description into Lua context\n"); // open all standard libraires luaL_openlibs(g_lua); // import socs if(!my_lua_import_soc(socs)) printf("Cannot import SoC descriptions into Lua context\n"); if(luaL_dofile(g_lua, lua_init)) printf("error in init: %s\n", lua_tostring(g_lua, -1)); lua_pop(g_lua, lua_gettop(g_lua)); /** start interactive mode */ if(!g_quiet) printf("Starting interactive lua session. Type 'help()' to get some help\n"); /** run startup commands */ for(size_t i = 0; i < startup_cmds.size(); i++) { bool ret = false; if(!g_quiet) printf("Running '%s'...\n", startup_cmds[i].second.c_str()); if(startup_cmds[i].first == exec_file) ret = luaL_dofile(g_lua, startup_cmds[i].second.c_str()); else if(startup_cmds[i].first == exec_cmd) ret = luaL_dostring(g_lua, startup_cmds[i].second.c_str()); if(ret) printf("error: %s\n", lua_tostring(g_lua, -1)); } // use readline to provide some history and completion rl_bind_key('\t', rl_complete); while(!g_exit) { char *input = readline("> "); if(!input) break; add_history(input); // evaluate string if(luaL_dostring(g_lua, input)) printf("error: %s\n", lua_tostring(g_lua, -1)); // pop everything to start from a clean stack lua_pop(g_lua, lua_gettop(g_lua)); free(input); } Lerr: // display log if handled if(!g_quiet) printf("Device log:\n"); print_log(g_hwdev); hwstub_release(g_hwdev); return 1; }