diff options
Diffstat (limited to 'ao-tools/lib')
-rw-r--r-- | ao-tools/lib/ao-editaltos.c | 95 | ||||
-rw-r--r-- | ao-tools/lib/ao-editaltos.h | 28 | ||||
-rw-r--r-- | ao-tools/lib/ao-selfload.c | 37 | ||||
-rw-r--r-- | ao-tools/lib/ao-selfload.h | 10 |
4 files changed, 153 insertions, 17 deletions
diff --git a/ao-tools/lib/ao-editaltos.c b/ao-tools/lib/ao-editaltos.c index 2a52c156..06009653 100644 --- a/ao-tools/lib/ao-editaltos.c +++ b/ao-tools/lib/ao-editaltos.c @@ -21,23 +21,23 @@ #include "ao-editaltos.h" struct ao_sym ao_symbols[] = { - { + [AO_ROMCONFIG_VERSION_INDEX] = { .name = "ao_romconfig_version", .required = 1 }, - { + [AO_ROMCONFIG_CHECK_INDEX] = { .name = "ao_romconfig_check", .required = 1 }, - { + [AO_SERIAL_NUMBER_INDEX] = { .name = "ao_serial_number", .required = 1 }, - { + [AO_RADIO_CAL_INDEX] = { .name = "ao_radio_cal", .required = 0 }, - { + [AO_USB_DESCRIPTORS_INDEX] = { .name = "ao_usb_descriptors", .required = 0 }, @@ -58,13 +58,6 @@ rewrite(struct ao_hex_image *load, unsigned address, uint8_t *data, int length) if (address < load->address || load->address + load->length < address + length) return false; - printf("rewrite %04x:", address); - for (i = 0; i < length; i++) - printf (" %02x", load->data[address - load->address + i]); - printf(" ->"); - for (i = 0; i < length; i++) - printf (" %02x", data[i]); - printf("\n"); memcpy(load->data + address - load->address, data, length); return true; } @@ -166,3 +159,81 @@ ao_editaltos(struct ao_hex_image *image, } return true; } + +static uint16_t +read_le16(uint8_t *src) +{ + return (uint16_t) src[0] | ((uint16_t) src[1] << 8); +} + +bool +ao_heximage_usb_id(struct ao_hex_image *image, struct ao_usb_id *id) +{ + uint32_t usb_descriptors; + + if (!AO_USB_DESCRIPTORS) + return false; + usb_descriptors = AO_USB_DESCRIPTORS - image->address; + + while (image->data[usb_descriptors] != 0 && usb_descriptors < image->length) { + if (image->data[usb_descriptors+1] == AO_USB_DESC_DEVICE) { + break; + } + usb_descriptors += image->data[usb_descriptors]; + } + + /* + * check to make sure there's at least 0x12 (size of a USB + * device descriptor) available + */ + if (usb_descriptors >= image->length || image->data[usb_descriptors] != 0x12) + return false; + + id->vid = read_le16(image->data + usb_descriptors + 8); + id->pid = read_le16(image->data + usb_descriptors + 10); + + return true; +} + +uint16_t * +ao_heximage_usb_product(struct ao_hex_image *image) +{ + uint32_t usb_descriptors; + int string_num; + uint16_t *product; + uint8_t product_len; + + if (!AO_USB_DESCRIPTORS) + return NULL; + usb_descriptors = AO_USB_DESCRIPTORS - image->address; + + string_num = 0; + while (image->data[usb_descriptors] != 0 && usb_descriptors < image->length) { + if (image->data[usb_descriptors+1] == AO_USB_DESC_STRING) { + ++string_num; + if (string_num == 3) + break; + } + usb_descriptors += image->data[usb_descriptors]; + } + + /* + * check to make sure there's at least 0x12 (size of a USB + * device descriptor) available + */ + if (usb_descriptors >= image->length || image->data[usb_descriptors] == 0) + return NULL; + + product_len = image->data[usb_descriptors] - 2; + + if (usb_descriptors < product_len + 2) + return NULL; + + product = malloc (product_len + 2); + if (!product) + return NULL; + + memcpy(product, image->data + usb_descriptors + 2, product_len); + product[product_len/2] = 0; + return product; +} diff --git a/ao-tools/lib/ao-editaltos.h b/ao-tools/lib/ao-editaltos.h index a480954b..6f2829b0 100644 --- a/ao-tools/lib/ao-editaltos.h +++ b/ao-tools/lib/ao-editaltos.h @@ -26,19 +26,31 @@ extern struct ao_sym ao_symbols[]; extern int ao_num_symbols; +#define AO_USB_DESC_DEVICE 1 #define AO_USB_DESC_STRING 3 -#define AO_ROMCONFIG_VERSION (ao_symbols[0].addr) -#define AO_ROMCONFIG_CHECK (ao_symbols[1].addr) -#define AO_SERIAL_NUMBER (ao_symbols[2].addr) -#define AO_RADIO_CAL (ao_symbols[3].addr) -#define AO_USB_DESCRIPTORS (ao_symbols[4].addr) +#define AO_ROMCONFIG_VERSION_INDEX 0 +#define AO_ROMCONFIG_CHECK_INDEX 1 +#define AO_SERIAL_NUMBER_INDEX 2 +#define AO_RADIO_CAL_INDEX 3 +#define AO_USB_DESCRIPTORS_INDEX 4 + +#define AO_ROMCONFIG_VERSION (ao_symbols[AO_ROMCONFIG_VERSION_INDEX].addr) +#define AO_ROMCONFIG_CHECK (ao_symbols[AO_ROMCONFIG_CHECK_INDEX].addr) +#define AO_SERIAL_NUMBER (ao_symbols[AO_SERIAL_NUMBER_INDEX].addr) +#define AO_RADIO_CAL (ao_symbols[AO_RADIO_CAL_INDEX].addr) +#define AO_USB_DESCRIPTORS (ao_symbols[AO_USB_DESCRIPTORS_INDEX].addr) struct ao_editaltos_funcs { uint16_t (*get_uint16)(void *closure, uint32_t addr); uint32_t (*get_uint32)(void *closure, uint32_t addr); }; +struct ao_usb_id { + uint16_t vid; + uint16_t pid; +}; + bool ao_editaltos_find_symbols(struct ao_sym *file_symbols, int num_file_symbols, struct ao_sym *symbols, int num_symbols); @@ -48,4 +60,10 @@ ao_editaltos(struct ao_hex_image *image, uint16_t serial, uint32_t radio_cal); +bool +ao_heximage_usb_id(struct ao_hex_image *image, struct ao_usb_id *id); + +uint16_t * +ao_heximage_usb_product(struct ao_hex_image *image); + #endif /* _AO_EDITALTOS_H_ */ diff --git a/ao-tools/lib/ao-selfload.c b/ao-tools/lib/ao-selfload.c index b4b878d1..0a23dfda 100644 --- a/ao-tools/lib/ao-selfload.c +++ b/ao-tools/lib/ao-selfload.c @@ -157,3 +157,40 @@ ao_self_get_uint32(struct cc_usb *cc, uint32_t addr) free(hex); return v; } + +bool +ao_self_get_usb_id(struct cc_usb *cc, struct ao_usb_id *id) +{ + struct ao_hex_image *hex; + bool ret; + + if (!AO_USB_DESCRIPTORS) + return false; + + hex = ao_self_read(cc, AO_USB_DESCRIPTORS, 512); + if (!hex) + return false; + + ret = ao_heximage_usb_id(hex, id); + free(hex); + return ret; +} + +uint16_t * +ao_self_get_usb_product(struct cc_usb *cc) +{ + struct ao_hex_image *hex; + uint16_t *ret; + + if (!AO_USB_DESCRIPTORS) + return NULL; + + hex = ao_self_read(cc, AO_USB_DESCRIPTORS, 512); + if (!hex) + return NULL; + + ret = ao_heximage_usb_product(hex); + free(hex); + return ret; +} + diff --git a/ao-tools/lib/ao-selfload.h b/ao-tools/lib/ao-selfload.h index 71529d1e..4aac5848 100644 --- a/ao-tools/lib/ao-selfload.h +++ b/ao-tools/lib/ao-selfload.h @@ -22,6 +22,7 @@ #include <stdbool.h> #include "ao-hex.h" #include "cc-usb.h" +#include "ao-editaltos.h" struct ao_hex_image * ao_self_read(struct cc_usb *cc, uint32_t address, uint32_t length); @@ -35,4 +36,13 @@ ao_self_get_uint16(struct cc_usb *cc, uint32_t addr); uint32_t ao_self_get_uint32(struct cc_usb *cc, uint32_t addr); +bool +ao_self_get_usb_id(struct cc_usb *cc, struct ao_usb_id *id); + +uint16_t * +ao_self_get_usb_product(struct cc_usb *cc); + +uint16_t * +ao_self_get_usb_product(struct cc_usb *cc); + #endif /* _AO_SELFLOAD_H_ */ |