Commit 4579a83a747b4d51ae20a099e425bb6bb55e1d54
Committed by
Gordon Hollingworth
1 parent
bae4aeb7
Added functionality to boot BCM2835 over the USB
Showing
4 changed files
with
180 additions
and
0 deletions
Makefile
0 → 100644
main.c
0 → 100755
| 1 | +#include "libusb-1.0/libusb.h" | |
| 2 | +#include <stdio.h> | |
| 3 | +#include <stdlib.h> | |
| 4 | + | |
| 5 | +#include <unistd.h> | |
| 6 | + | |
| 7 | +int Initialize_Device(libusb_context ** ctx, libusb_device_handle ** usb_device) | |
| 8 | +{ | |
| 9 | + int ret = 0; | |
| 10 | + | |
| 11 | + *usb_device = libusb_open_device_with_vid_pid(*ctx, 0x0a5c, 0x2763); | |
| 12 | + if (*usb_device == NULL) | |
| 13 | + { | |
| 14 | + return -1; | |
| 15 | + } | |
| 16 | + | |
| 17 | + ret = libusb_claim_interface(*usb_device, 0); | |
| 18 | + if (ret) | |
| 19 | + { | |
| 20 | + printf("Failed to claim interface\n"); | |
| 21 | + return ret; | |
| 22 | + } | |
| 23 | + | |
| 24 | + return ret; | |
| 25 | +} | |
| 26 | + | |
| 27 | +int ep_write(unsigned char *buf, int len, libusb_device_handle * usb_device) | |
| 28 | +{ | |
| 29 | + int ret = | |
| 30 | + libusb_control_transfer(usb_device, LIBUSB_REQUEST_TYPE_VENDOR, 0, | |
| 31 | + len & 0xffff, len >> 16, NULL, 0, 1000); | |
| 32 | + int a_len; | |
| 33 | + | |
| 34 | + libusb_bulk_transfer(usb_device, 0x01, buf, len, &a_len, 1000); | |
| 35 | + | |
| 36 | + return a_len; | |
| 37 | +} | |
| 38 | + | |
| 39 | +int ep_read(unsigned char *buf, int len, libusb_device_handle * usb_device) | |
| 40 | +{ | |
| 41 | + int ret = | |
| 42 | + libusb_control_transfer(usb_device, | |
| 43 | + LIBUSB_REQUEST_TYPE_VENDOR | | |
| 44 | + LIBUSB_ENDPOINT_IN, 0, len & 0xffff, | |
| 45 | + len >> 16, buf, len, 1000); | |
| 46 | + | |
| 47 | + return len; | |
| 48 | +} | |
| 49 | + | |
| 50 | +int main(int argc, char *argv[]) | |
| 51 | +{ | |
| 52 | + int result; | |
| 53 | + libusb_context *ctx; | |
| 54 | + libusb_device_handle *usb_device; | |
| 55 | + unsigned char *txbuf; | |
| 56 | + int size; | |
| 57 | + int retcode; | |
| 58 | + FILE *fp1, *fp2; | |
| 59 | + char def1[] = "usbbootcode.bin"; | |
| 60 | + char def2[] = "msd.bin"; | |
| 61 | + char *str1, *str2; | |
| 62 | + struct MESSAGE_S { | |
| 63 | + int length; | |
| 64 | + unsigned char signature[20]; | |
| 65 | + } message; | |
| 66 | + | |
| 67 | + if (argc < 3) | |
| 68 | + { | |
| 69 | + str1 = def1; | |
| 70 | + str2 = def2; | |
| 71 | + } else | |
| 72 | + { | |
| 73 | + str1 = argv[1]; | |
| 74 | + str2 = argv[2]; | |
| 75 | + } | |
| 76 | + | |
| 77 | + fp1 = fopen(str1, "rb"); | |
| 78 | + if (fp1 == NULL) | |
| 79 | + { | |
| 80 | + printf("Cannot open file %s\n", str1); | |
| 81 | + exit(-1); | |
| 82 | + } | |
| 83 | + | |
| 84 | + fp2 = fopen(str2, "rb"); | |
| 85 | + if (fp2 == NULL) | |
| 86 | + { | |
| 87 | + printf("Cannot open file %s\n", str2); | |
| 88 | + exit(-1); | |
| 89 | + } | |
| 90 | + | |
| 91 | + int ret = libusb_init(&ctx); | |
| 92 | + if (ret) | |
| 93 | + { | |
| 94 | + printf("Failed to initialise libUSB\n"); | |
| 95 | + exit(-1); | |
| 96 | + } | |
| 97 | + | |
| 98 | + libusb_set_debug(ctx, 0); | |
| 99 | + | |
| 100 | + while (1) | |
| 101 | + { | |
| 102 | + FILE *fp; | |
| 103 | + | |
| 104 | + printf("Waiting for BCM2835 ...\n"); | |
| 105 | + | |
| 106 | + // Wait for a device to get plugged in | |
| 107 | + do | |
| 108 | + { | |
| 109 | + result = Initialize_Device(&ctx, &usb_device); | |
| 110 | + if (result) | |
| 111 | + { | |
| 112 | + sleep(1); | |
| 113 | + } | |
| 114 | + | |
| 115 | + } | |
| 116 | + while (result); | |
| 117 | + | |
| 118 | + { | |
| 119 | + struct libusb_device_descriptor desc; | |
| 120 | + ret = | |
| 121 | + libusb_get_device_descriptor(libusb_get_device | |
| 122 | + (usb_device), &desc); | |
| 123 | + printf("Found serial = %d: writing file %s\n", | |
| 124 | + desc.iSerialNumber, | |
| 125 | + desc.iSerialNumber == 0 ? str1 : str2); | |
| 126 | + fp = desc.iSerialNumber == 0 ? fp1 : fp2; | |
| 127 | + } | |
| 128 | + | |
| 129 | + fseek(fp, 0, SEEK_END); | |
| 130 | + message.length = ftell(fp); | |
| 131 | + fseek(fp, 0, SEEK_SET); | |
| 132 | + | |
| 133 | + txbuf = (unsigned char *)malloc(message.length); | |
| 134 | + if (txbuf == NULL) | |
| 135 | + { | |
| 136 | + printf("Failed to allocate memory\n"); | |
| 137 | + exit(-1); | |
| 138 | + } | |
| 139 | + | |
| 140 | + fread(txbuf, 1, message.length, fp); | |
| 141 | + | |
| 142 | + size = | |
| 143 | + ep_write((unsigned char *)&message, sizeof(message), | |
| 144 | + usb_device); | |
| 145 | + if (size != sizeof(message)) | |
| 146 | + { | |
| 147 | + printf("Failed to write correct length, returned %d\n", | |
| 148 | + size); | |
| 149 | + exit(-1); | |
| 150 | + } | |
| 151 | + | |
| 152 | + size = ep_write(txbuf, message.length, usb_device); | |
| 153 | + if (size != message.length) | |
| 154 | + { | |
| 155 | + printf("Failed to read correct length, returned %d\n", | |
| 156 | + size); | |
| 157 | + exit(-1); | |
| 158 | + } | |
| 159 | + | |
| 160 | + sleep(1); | |
| 161 | + | |
| 162 | + size = | |
| 163 | + ep_read((unsigned char *)&retcode, sizeof(retcode), | |
| 164 | + usb_device); | |
| 165 | + | |
| 166 | + if (retcode == 0) | |
| 167 | + printf("Successfull\n"); | |
| 168 | + else | |
| 169 | + printf("Failed : 0x%x", retcode); | |
| 170 | + | |
| 171 | + sleep(1); | |
| 172 | + libusb_close(usb_device); | |
| 173 | + } | |
| 174 | + | |
| 175 | + libusb_exit(ctx); | |
| 176 | + | |
| 177 | + return 0; | |
| 178 | +} | ... | ... |
msd.bin
0 → 100755
No preview for this file type
usbbootcode.bin
0 → 100755
No preview for this file type