ao-usbload: Check target device name to avoid mis-flashing
[fw/altos] / ao-tools / lib / ao-editaltos.c
index 2a52c15633e91859e0f2ec0d65041bc5af62f880..0600965309834527964843bedd596821ae93d40a 100644 (file)
 #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;
+}