| 1 | // |
| 2 | // Authors: Wolfgang Spraul <wolfgang@qi-hardware.com> |
| 3 | // |
| 4 | // This program is free software; you can redistribute it and/or |
| 5 | // modify it under the terms of the GNU General Public License |
| 6 | // as published by the Free Software Foundation; either version |
| 7 | // 3 of the License, or (at your option) any later version. |
| 8 | // |
| 9 | |
| 10 | #include <stdio.h> |
| 11 | #include <string.h> |
| 12 | #include <usb.h> |
| 13 | #include "xbboot_version.h" |
| 14 | |
| 15 | #define HIWORD(dw) (((dw) >> 16) & 0xFFFF) |
| 16 | #define LOWORD(dw) ((dw) & 0xFFFF) |
| 17 | |
| 18 | #define INGENIC_VENDOR_ID 0x601A |
| 19 | #define INGENIC_XBURST_USBBOOT 0x4740 |
| 20 | |
| 21 | // REQ_ values are negative so they can be mixed together with the VR_ types in a signed integer. |
| 22 | #define REQ_BULK_READ -1 |
| 23 | #define REQ_BULK_WRITE -2 |
| 24 | |
| 25 | #define VR_GET_CPU_INFO 0x00 |
| 26 | #define VR_SET_DATA_ADDRESS 0x01 |
| 27 | #define VR_SET_DATA_LENGTH 0x02 |
| 28 | #define VR_FLUSH_CACHES 0x03 |
| 29 | #define VR_PROGRAM_START1 0x04 |
| 30 | #define VR_PROGRAM_START2 0x05 |
| 31 | #define VR_NOR_OPS 0x06 |
| 32 | #define VR_NAND_OPS 0x07 |
| 33 | #define VR_SDRAM_OPS 0x08 |
| 34 | #define VR_CONFIGRATION 0x09 |
| 35 | #define VR_GET_NUM 0x0a |
| 36 | |
| 37 | #define USB_TIMEOUT 3000 |
| 38 | #define VR_GET_CPU_INFO_LEN 8 |
| 39 | #define INGENIC_IN_ENDPOINT 0x81 |
| 40 | #define INGENIC_OUT_ENDPOINT 0x01 |
| 41 | |
| 42 | int main(int argc, char** argv) |
| 43 | { |
| 44 | struct usb_device* xburst_dev = 0; |
| 45 | uint8_t xburst_interface = 0; |
| 46 | struct usb_dev_handle* xburst_h = 0; |
| 47 | int request_type, usb_status; |
| 48 | |
| 49 | if (argc < 2 |
| 50 | || !strcmp(argv[1], "-h") |
| 51 | || !strcmp(argv[1], "--help")) { |
| 52 | printf("\n" |
| 53 | "xbboot version %s - Ingenic XBurst USB Boot Vendor Requests\n" |
| 54 | "(c) 2009 Wolfgang Spraul\n" |
| 55 | "Report bugs to <wolfgang@qi-hardware.com>.\n" |
| 56 | "\n" |
| 57 | "xbboot [vendor_request] ... (must run as root)\n" |
| 58 | " -h --help print this help message\n" |
| 59 | " -v --version print the version number\n" |
| 60 | "\n" |
| 61 | " bulk_read <len> read len bulk bytes from USB, write to stdout\n" |
| 62 | " bulk_write <path> write file at <path> to USB\n" |
| 63 | " [get_info | VR_GET_CPU_INFO] read 8-byte CPU info and write to stdout\n" |
| 64 | " [set_addr | VR_SET_DATA_ADDRESS] <addr> send memory address\n" |
| 65 | " [set_len | VR_SET_DATA_LENGTH] <len> send data length\n" |
| 66 | " [flush_cache | VR_FLUSH_CACHES] flush I-Cache and D-Cache\n" |
| 67 | " [start1 | VR_PROGRAM_START1] <addr> transfer data from D-Cache to I-Cache and branch to I-Cache\n" |
| 68 | " [start2 | VR_PROGRAM_START2] <addr> branch to <addr> directly\n" |
| 69 | "\n" |
| 70 | "- all numbers can be prefixed 0x for hex otherwise decimal\n" |
| 71 | "\n", XBBOOT_VERSION); |
| 72 | // stage1: 0x80002000 |
| 73 | // stage2: 0x81C00000 |
| 74 | // u-boot: 0x80600000 |
| 75 | // uImage: 0x80010000 |
| 76 | return EXIT_SUCCESS; |
| 77 | } |
| 78 | if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version")) { |
| 79 | printf("xbboot version %s\n", XBBOOT_VERSION); |
| 80 | return EXIT_SUCCESS; |
| 81 | } |
| 82 | if (!strcmp(argv[1], "bulk_read")) |
| 83 | request_type = REQ_BULK_READ; |
| 84 | else if (!strcmp(argv[1], "bulk_write")) |
| 85 | request_type = REQ_BULK_WRITE; |
| 86 | else if (!strcmp(argv[1], "VR_GET_CPU_INFO") || !strcmp(argv[1], "get_info")) |
| 87 | request_type = VR_GET_CPU_INFO; |
| 88 | else if (!strcmp(argv[1], "VR_SET_DATA_ADDRESS") || !strcmp(argv[1], "set_addr")) |
| 89 | request_type = VR_SET_DATA_ADDRESS; |
| 90 | else if (!strcmp(argv[1], "VR_SET_DATA_LENGTH") || !strcmp(argv[1], "set_len")) |
| 91 | request_type = VR_SET_DATA_LENGTH; |
| 92 | else if (!strcmp(argv[1], "VR_FLUSH_CACHES") || !strcmp(argv[1], "flush_cache")) |
| 93 | request_type = VR_FLUSH_CACHES; |
| 94 | else if (!strcmp(argv[1], "VR_PROGRAM_START1") || !strcmp(argv[1], "start1")) |
| 95 | request_type = VR_PROGRAM_START1; |
| 96 | else if (!strcmp(argv[1], "VR_PROGRAM_START2") || !strcmp(argv[1], "start2")) |
| 97 | request_type = VR_PROGRAM_START2; |
| 98 | else { |
| 99 | fprintf(stderr, "Error - unknown vendor request %s - run with --help to see all requests\n", argv[1]); |
| 100 | return EXIT_FAILURE; |
| 101 | } |
| 102 | if ((getuid()) || (getgid())) { |
| 103 | fprintf(stderr, "Error - you must be root to run '%s'\n", argv[0]); |
| 104 | return EXIT_FAILURE; |
| 105 | } |
| 106 | |
| 107 | usb_init(); |
| 108 | usb_find_busses(); |
| 109 | usb_find_devices(); |
| 110 | |
| 111 | // look for Ingenic XBurst USB boot device & interface |
| 112 | { |
| 113 | { |
| 114 | struct usb_bus* usb_bus; |
| 115 | struct usb_device* usb_dev; |
| 116 | |
| 117 | for (usb_bus = usb_get_busses(); usb_bus != 0; usb_bus = usb_bus->next) { |
| 118 | for (usb_dev = usb_bus->devices; usb_dev != 0; usb_dev = usb_dev->next) { |
| 119 | if (usb_dev->descriptor.idVendor == INGENIC_VENDOR_ID |
| 120 | && usb_dev->descriptor.idProduct == INGENIC_XBURST_USBBOOT) { |
| 121 | if (xburst_dev) { |
| 122 | fprintf(stderr, "Error - more than one XBurst boot device found.\n"); |
| 123 | goto xout; |
| 124 | } |
| 125 | xburst_dev = usb_dev; |
| 126 | // keep searching to make sure there is only 1 XBurst device |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | if (!xburst_dev) { |
| 131 | fprintf(stderr, "Error - no XBurst boot device found.\n"); |
| 132 | goto xout; |
| 133 | } |
| 134 | } |
| 135 | { |
| 136 | struct usb_config_descriptor* usb_config_desc; |
| 137 | struct usb_interface_descriptor* usb_if_desc; |
| 138 | struct usb_interface* usb_if; |
| 139 | int cfg_index, if_index, alt_index; |
| 140 | |
| 141 | for (cfg_index = 0; cfg_index < xburst_dev->descriptor.bNumConfigurations; cfg_index++) { |
| 142 | usb_config_desc = &xburst_dev->config[cfg_index]; |
| 143 | if (!usb_config_desc) { |
| 144 | fprintf(stderr, "Error - usb_config_desc NULL\n"); |
| 145 | goto xout; |
| 146 | } |
| 147 | for (if_index = 0; if_index < usb_config_desc->bNumInterfaces; if_index++) { |
| 148 | usb_if = &usb_config_desc->interface[if_index]; |
| 149 | if (!usb_if) { |
| 150 | fprintf(stderr, "Error - usb_if NULL\n"); |
| 151 | goto xout; |
| 152 | } |
| 153 | for (alt_index = 0; alt_index < usb_if->num_altsetting; alt_index++) { |
| 154 | usb_if_desc = &usb_if->altsetting[alt_index]; |
| 155 | if (!usb_if_desc) { |
| 156 | fprintf(stderr, "Error - usb_if_desc NULL\n"); |
| 157 | goto xout; |
| 158 | } |
| 159 | if (usb_if_desc->bInterfaceClass == 0xFF |
| 160 | && usb_if_desc->bInterfaceSubClass == 0) { |
| 161 | xburst_interface = usb_if_desc->bInterfaceNumber; |
| 162 | goto interface_found; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | interface_found: ; |
| 168 | } |
| 169 | } |
| 170 | xburst_h = usb_open(xburst_dev); |
| 171 | if (!xburst_h) { |
| 172 | fprintf(stderr, "Error - can't open XBurst device: %s\n", usb_strerror()); |
| 173 | goto xout; |
| 174 | } |
| 175 | if (usb_claim_interface(xburst_h, xburst_interface) < 0) { |
| 176 | fprintf(stderr, "Error - can't claim XBurst interface: %s\n", usb_strerror()); |
| 177 | goto xout_xburst_h; |
| 178 | } |
| 179 | switch (request_type) { |
| 180 | case VR_GET_CPU_INFO: { |
| 181 | char cpu_info_buf[VR_GET_CPU_INFO_LEN+1] = {0}; |
| 182 | usb_status = usb_control_msg(xburst_h, |
| 183 | /* requesttype */ USB_ENDPOINT_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 184 | /* request */ VR_GET_CPU_INFO, |
| 185 | /* value */ 0, |
| 186 | /* index */ 0, |
| 187 | /* bytes */ cpu_info_buf, |
| 188 | /* size */ VR_GET_CPU_INFO_LEN, |
| 189 | /* timeout */ USB_TIMEOUT); |
| 190 | if (usb_status != VR_GET_CPU_INFO_LEN) { |
| 191 | fprintf(stderr, "Error - %s() returned %i\n", argv[1], usb_status); |
| 192 | goto xout_xburst_interface; |
| 193 | } |
| 194 | printf("VR_GET_CPU_INFO %s\n", cpu_info_buf); |
| 195 | break; |
| 196 | } |
| 197 | case VR_SET_DATA_ADDRESS: |
| 198 | case VR_SET_DATA_LENGTH: |
| 199 | case VR_PROGRAM_START1: |
| 200 | case VR_PROGRAM_START2: { |
| 201 | uint32_t u32_param; |
| 202 | if (argc != 3) { |
| 203 | fprintf(stderr, "Error - number of %s parameters %i\n", argv[1], argc); |
| 204 | goto xout_xburst_interface; |
| 205 | } |
| 206 | u32_param = strtoul(argv[2], 0 /* endptr */, 0 /* base */); |
| 207 | |
| 208 | usb_status = usb_control_msg(xburst_h, |
| 209 | /* requesttype */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 210 | /* request */ request_type, |
| 211 | /* value */ HIWORD(u32_param), |
| 212 | /* index */ LOWORD(u32_param), |
| 213 | /* bytes */ 0, |
| 214 | /* size */ 0, |
| 215 | /* timeout */ USB_TIMEOUT); |
| 216 | if (usb_status) { |
| 217 | fprintf(stderr, "Error - %s() returned %i\n", argv[1], usb_status); |
| 218 | goto xout_xburst_interface; |
| 219 | } |
| 220 | printf("%s %lxh\n", argv[1], (unsigned long) u32_param); |
| 221 | break; |
| 222 | } |
| 223 | case VR_FLUSH_CACHES: { |
| 224 | usb_status = usb_control_msg(xburst_h, |
| 225 | /* requesttype */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 226 | /* request */ VR_FLUSH_CACHES, |
| 227 | /* value */ 0, |
| 228 | /* index */ 0, |
| 229 | /* bytes */ 0, |
| 230 | /* size */ 0, |
| 231 | /* timeout */ USB_TIMEOUT); |
| 232 | if (usb_status) { |
| 233 | fprintf(stderr, "Error - %s() returned %i\n", argv[1], usb_status); |
| 234 | goto xout_xburst_interface; |
| 235 | } |
| 236 | printf("VR_FLUSH_CACHES\n"); |
| 237 | break; |
| 238 | } |
| 239 | case REQ_BULK_READ: { |
| 240 | int read_len; |
| 241 | char* read_buf; |
| 242 | |
| 243 | if (argc != 3) { |
| 244 | fprintf(stderr, "Error - number of %s parameters %i\n", argv[1], argc); |
| 245 | goto xout_xburst_interface; |
| 246 | } |
| 247 | if (argv[2][0] == '0' && argv[2][1] == 'x') |
| 248 | read_len = strtol(&argv[2][2], 0 /* endptr */, 16 /* base */); |
| 249 | else |
| 250 | read_len = strtol(argv[2], 0 /* endptr */, 10 /* base */); |
| 251 | read_buf = (char*) malloc(read_len); |
| 252 | if (!read_buf) { |
| 253 | fprintf(stderr, "Error - cannot allocate %i bytes read buffer.\n", read_len); |
| 254 | goto xout_xburst_interface; |
| 255 | } |
| 256 | usb_status = usb_bulk_read(xburst_h, |
| 257 | /* endpoint */ INGENIC_IN_ENDPOINT, |
| 258 | /* bytes */ read_buf, |
| 259 | /* size */ read_len, |
| 260 | /* timeout */ USB_TIMEOUT); |
| 261 | if (usb_status > 0) |
| 262 | fwrite(read_buf, 1 /* size */, usb_status, stdout); |
| 263 | free(read_buf); |
| 264 | if (usb_status < read_len) { |
| 265 | fprintf(stderr, "Error reading %d bytes (result %i).\n", read_len, usb_status); |
| 266 | goto xout_xburst_interface; |
| 267 | } |
| 268 | break; |
| 269 | } |
| 270 | case REQ_BULK_WRITE: { |
| 271 | char* file_data; |
| 272 | int file_len; |
| 273 | FILE* file_h; |
| 274 | size_t num_read; |
| 275 | |
| 276 | if (argc != 3) { |
| 277 | fprintf(stderr, "Error - number of parameters %i\n", argc); |
| 278 | goto xout_xburst_interface; |
| 279 | } |
| 280 | file_h = fopen(argv[2], "rb"); |
| 281 | if (!file_h) { |
| 282 | fprintf(stderr, "Error opening %s.\n", argv[2]); |
| 283 | goto xout_xburst_interface; |
| 284 | } |
| 285 | if (fseek(file_h, 0, SEEK_END)) { |
| 286 | fprintf(stderr, "Error seeking to end of %s.\n", argv[2]); |
| 287 | fclose(file_h); |
| 288 | goto xout_xburst_interface; |
| 289 | } |
| 290 | file_len = ftell(file_h); |
| 291 | if (fseek(file_h, 0, SEEK_SET)) { |
| 292 | fprintf(stderr, "Error seeking to beginning of %s.\n", argv[2]); |
| 293 | fclose(file_h); |
| 294 | goto xout_xburst_interface; |
| 295 | } |
| 296 | file_data = (char*) malloc(file_len); |
| 297 | if (!file_data) { |
| 298 | fprintf(stderr, "Error allocating %d bytes data.\n", file_len); |
| 299 | fclose(file_h); |
| 300 | goto xout_xburst_interface; |
| 301 | } |
| 302 | num_read = fread(file_data, 1, file_len, file_h); |
| 303 | fclose(file_h); |
| 304 | if (num_read != (size_t) file_len) { |
| 305 | fprintf(stderr, "Error reading %d bytes (got %d).\n", file_len, num_read); |
| 306 | free(file_data); |
| 307 | goto xout_xburst_interface; |
| 308 | } |
| 309 | usb_status = usb_bulk_write(xburst_h, |
| 310 | /* endpoint */ INGENIC_OUT_ENDPOINT, |
| 311 | /* bytes */ file_data, |
| 312 | /* size */ file_len, |
| 313 | /* timeout */ USB_TIMEOUT); |
| 314 | free(file_data); |
| 315 | if (usb_status < file_len) { |
| 316 | fprintf(stderr, "Error writing %d bytes (result %i).\n", file_len, usb_status); |
| 317 | goto xout_xburst_interface; |
| 318 | } |
| 319 | printf("bulk_write successfully wrote %i bytes.\n", usb_status); |
| 320 | break; |
| 321 | } |
| 322 | } |
| 323 | usb_release_interface(xburst_h, xburst_interface); |
| 324 | usb_close(xburst_h); |
| 325 | return EXIT_SUCCESS; |
| 326 | |
| 327 | xout_xburst_interface: |
| 328 | usb_release_interface(xburst_h, xburst_interface); |
| 329 | xout_xburst_h: |
| 330 | usb_close(xburst_h); |
| 331 | xout: |
| 332 | return EXIT_FAILURE; |
| 333 | } |
| 334 | |