Commit be1bc6424999ba0a013c129bf57bfba2a60b290a
Committed by
Gordon Hollingworth
1 parent
8d709206
Add buildroot.elf installation and cmdline parameters
Showing
2 changed files
with
94 additions
and
33 deletions
Makefile
main.c
| ... | ... | @@ -5,6 +5,30 @@ |
| 5 | 5 | |
| 6 | 6 | #include <unistd.h> |
| 7 | 7 | |
| 8 | +int verbose = 0; | |
| 9 | + | |
| 10 | +void usage(int error) | |
| 11 | +{ | |
| 12 | + FILE * dest = error ? stderr : stdout; | |
| 13 | + | |
| 14 | + fprintf(dest, "Usage: rpiboot\n"); | |
| 15 | + fprintf(dest, " or: rpiboot -b fatimage\n"); | |
| 16 | + fprintf(dest, "Boot a Raspberry Pi Model A or Compute Module through USB\n"); | |
| 17 | + fprintf(dest, "\n"); | |
| 18 | + fprintf(dest, "rpiboot : Boot the device into Mass Storage Device mode\n"); | |
| 19 | + fprintf(dest, "rpiboot -b fatimage : Boot the device into a buildroot linux image\n"); | |
| 20 | + fprintf(dest, "\n"); | |
| 21 | + fprintf(dest, "Further options:\n"); | |
| 22 | + fprintf(dest, " -x executable : Autoexecute function\n"); | |
| 23 | + fprintf(dest, " This option is used to trigger the execution of a\n"); | |
| 24 | + fprintf(dest, " script after finishing the USB boot process\n"); | |
| 25 | + fprintf(dest, " -l : Sit in a loop permanently\n"); | |
| 26 | + fprintf(dest, " -v : Verbose output"); | |
| 27 | + fprintf(dest, " -h : This help\n"); | |
| 28 | + exit(-1); | |
| 29 | +} | |
| 30 | + | |
| 31 | + | |
| 8 | 32 | int Initialize_Device(libusb_context ** ctx, libusb_device_handle ** usb_device) |
| 9 | 33 | { |
| 10 | 34 | int ret = 0; |
| ... | ... | @@ -33,7 +57,8 @@ int ep_write(unsigned char *buf, int len, libusb_device_handle * usb_device) |
| 33 | 57 | int a_len; |
| 34 | 58 | |
| 35 | 59 | ret = libusb_bulk_transfer(usb_device, 0x01, buf, len, &a_len, 100000); |
| 36 | - printf("libusb_bulk_transfer returned %d\n", ret); | |
| 60 | + if(verbose) | |
| 61 | + printf("libusb_bulk_transfer returned %d\n", ret); | |
| 37 | 62 | |
| 38 | 63 | return a_len; |
| 39 | 64 | } |
| ... | ... | @@ -57,27 +82,57 @@ int main(int argc, char *argv[]) |
| 57 | 82 | unsigned char *txbuf; |
| 58 | 83 | int size; |
| 59 | 84 | int retcode; |
| 60 | - FILE *fp1, *fp2; | |
| 85 | + int last_serial = -1; | |
| 86 | + FILE *fp1, *fp2, *fp; | |
| 61 | 87 | char def1[] = "/usr/share/rpiboot/usbbootcode.bin"; |
| 62 | 88 | char def2[] = "/usr/share/rpiboot/msd.elf"; |
| 89 | + char def3[] = "/usr/share/rpiboot/buildroot.elf"; | |
| 63 | 90 | |
| 64 | - char *stage1, *stage2; | |
| 65 | - char *dir; | |
| 91 | + char *stage1 = def1, *stage2 = def2; | |
| 92 | + char *fatimage = NULL, *executable = NULL; | |
| 93 | + int loop = 0; | |
| 66 | 94 | |
| 67 | 95 | struct MESSAGE_S { |
| 68 | 96 | int length; |
| 69 | 97 | unsigned char signature[20]; |
| 70 | 98 | } message; |
| 71 | 99 | |
| 72 | - if(argc > 2) | |
| 73 | - { | |
| 74 | - stage1 = argv[1]; | |
| 75 | - stage2 = argv[2]; | |
| 76 | - } | |
| 77 | - else | |
| 100 | + // Skip the command name | |
| 101 | + argv++; argc--; | |
| 102 | + while(*argv) | |
| 78 | 103 | { |
| 79 | - stage1 = def1; | |
| 80 | - stage2 = def2; | |
| 104 | + if(strcmp(*argv, "-b") == 0) | |
| 105 | + { | |
| 106 | + argv++; argc--; | |
| 107 | + if(argc < 1) | |
| 108 | + usage(1); | |
| 109 | + stage1 = def1; | |
| 110 | + stage2 = def3; | |
| 111 | + fatimage = *argv; | |
| 112 | + } | |
| 113 | + else if(strcmp(*argv, "-h") == 0 || strcmp(*argv, "--help") == 0) | |
| 114 | + { | |
| 115 | + usage(0); | |
| 116 | + } | |
| 117 | + else if(strcmp(*argv, "-x") == 0) | |
| 118 | + { | |
| 119 | + argv++; argc--; | |
| 120 | + executable = *argv; | |
| 121 | + } | |
| 122 | + else if(strcmp(*argv, "-l") == 0) | |
| 123 | + { | |
| 124 | + loop = 1; | |
| 125 | + } | |
| 126 | + else if(strcmp(*argv, "-v") == 0) | |
| 127 | + { | |
| 128 | + verbose = 1; | |
| 129 | + } | |
| 130 | + else | |
| 131 | + { | |
| 132 | + usage(1); | |
| 133 | + } | |
| 134 | + | |
| 135 | + argv++; argc--; | |
| 81 | 136 | } |
| 82 | 137 | |
| 83 | 138 | fp1 = fopen(stage1, "rb"); |
| ... | ... | @@ -108,9 +163,9 @@ int main(int argc, char *argv[]) |
| 108 | 163 | |
| 109 | 164 | libusb_set_debug(ctx, 0); |
| 110 | 165 | |
| 111 | - while (1) | |
| 166 | + do | |
| 112 | 167 | { |
| 113 | - FILE *fp, *fp_img = NULL; | |
| 168 | + FILE *fp_img = NULL; | |
| 114 | 169 | struct libusb_device_descriptor desc; |
| 115 | 170 | |
| 116 | 171 | printf("Waiting for BCM2835 ...\n"); |
| ... | ... | @@ -119,17 +174,26 @@ int main(int argc, char *argv[]) |
| 119 | 174 | do |
| 120 | 175 | { |
| 121 | 176 | result = Initialize_Device(&ctx, &usb_device); |
| 122 | - if (result) | |
| 177 | + if(result == 0) | |
| 123 | 178 | { |
| 124 | - sleep(1); | |
| 179 | + libusb_get_device_descriptor(libusb_get_device | |
| 180 | + (usb_device), &desc); | |
| 181 | + // Make sure we've re-enumerated since the last time | |
| 182 | + if(desc.iSerialNumber == last_serial) | |
| 183 | + { | |
| 184 | + result = -1; | |
| 185 | + libusb_close(usb_device); | |
| 186 | + } | |
| 125 | 187 | } |
| 126 | 188 | |
| 189 | + if (result) | |
| 190 | + { | |
| 191 | + usleep(100); | |
| 192 | + } | |
| 127 | 193 | } |
| 128 | 194 | while (result); |
| 129 | 195 | |
| 130 | - ret = | |
| 131 | - libusb_get_device_descriptor(libusb_get_device | |
| 132 | - (usb_device), &desc); | |
| 196 | + last_serial = desc.iSerialNumber; | |
| 133 | 197 | printf("Found serial = %d: writing file %s\n", |
| 134 | 198 | desc.iSerialNumber, |
| 135 | 199 | desc.iSerialNumber == 0 ? stage1 : stage2); |
| ... | ... | @@ -139,20 +203,18 @@ int main(int argc, char *argv[]) |
| 139 | 203 | message.length = ftell(fp); |
| 140 | 204 | fseek(fp, 0, SEEK_SET); |
| 141 | 205 | |
| 142 | - printf("Writing %d bytes of program data\n", message.length); | |
| 143 | - | |
| 144 | - if(desc.iSerialNumber == 1 && argc == 4) | |
| 206 | + if(desc.iSerialNumber == 1 && fatimage != NULL) | |
| 145 | 207 | { |
| 146 | 208 | // Been given a filesystem image |
| 147 | - fp_img = fopen(argv[3], "rb"); | |
| 209 | + fp_img = fopen(fatimage, "rb"); | |
| 148 | 210 | if(fp_img == NULL) |
| 149 | 211 | { |
| 150 | - printf("Failed to open image %s\n", argv[3]); | |
| 212 | + printf("Failed to open image %s\n", fatimage); | |
| 151 | 213 | exit(-1); |
| 152 | 214 | } |
| 153 | 215 | fseek(fp_img, 0, SEEK_END); |
| 154 | 216 | message.length += ftell(fp_img); |
| 155 | - printf("Adding %d bytes of binary to end of elf\n", ftell(fp_img)); | |
| 217 | + if(verbose) printf("Adding %d bytes of binary to end of elf\n", ftell(fp_img)); | |
| 156 | 218 | fseek(fp_img, 0, SEEK_SET); |
| 157 | 219 | } |
| 158 | 220 | |
| ... | ... | @@ -178,7 +240,7 @@ int main(int argc, char *argv[]) |
| 178 | 240 | size); |
| 179 | 241 | exit(-1); |
| 180 | 242 | } |
| 181 | - | |
| 243 | + if(verbose) printf("Writing %d bytes\n", message.length); | |
| 182 | 244 | size = ep_write(txbuf, message.length, usb_device); |
| 183 | 245 | if (size != message.length) |
| 184 | 246 | { |
| ... | ... | @@ -187,27 +249,25 @@ int main(int argc, char *argv[]) |
| 187 | 249 | exit(-1); |
| 188 | 250 | } |
| 189 | 251 | |
| 190 | - sleep(1); | |
| 191 | - | |
| 192 | 252 | size = |
| 193 | 253 | ep_read((unsigned char *)&retcode, sizeof(retcode), |
| 194 | 254 | usb_device); |
| 195 | 255 | |
| 196 | 256 | if (retcode == 0) |
| 197 | 257 | { |
| 198 | - printf("Successful\n"); | |
| 199 | - if(fp==fp2) | |
| 258 | + if(verbose) printf("Successful\n"); | |
| 259 | + | |
| 260 | + if(fp == fp2 && executable) | |
| 200 | 261 | { |
| 201 | - printf("Raspberry Pi is now a mass storage device, use lsblk to find it\n"); | |
| 202 | - exit(0); | |
| 262 | + system(executable); | |
| 203 | 263 | } |
| 204 | 264 | } |
| 205 | 265 | else |
| 206 | 266 | printf("Failed : 0x%x", retcode); |
| 207 | 267 | |
| 208 | - sleep(1); | |
| 209 | 268 | libusb_close(usb_device); |
| 210 | 269 | } |
| 270 | + while(fp == fp1 || loop); | |
| 211 | 271 | |
| 212 | 272 | libusb_exit(ctx); |
| 213 | 273 | ... | ... |