/ bitforce-firmware-flash.c
bitforce-firmware-flash.c
1 /* 2 * Copyright 2012 Luke Dashjr 3 * 4 * This program is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License as published by the Free 6 * Software Foundation; either version 3 of the License, or (at your option) 7 * any later version. See COPYING for more details. 8 */ 9 10 #define _BSD_SOURCE 11 #include <stdint.h> 12 #include <stdio.h> 13 #include <string.h> 14 15 #include <libgen.h> 16 #include <arpa/inet.h> 17 18 #define BFL_FILE_MAGIC "BFLDATA" 19 #define BFL_UPLOAD_MAGIC "NGH-STREAM" 20 21 #define myassert(expr, n, ...) \ 22 do { \ 23 if (!(expr)) { \ 24 fprintf(stderr, __VA_ARGS__); \ 25 return n; \ 26 } \ 27 } while(0) 28 29 #define ERRRESP(buf) buf, (buf[strlen(buf)-1] == '\n' ? "" : "\n") 30 31 #define WAITFOROK(n, msg) \ 32 do { \ 33 myassert(fgets(buf, sizeof(buf), BFL), n, "Error reading response from " msg "\n"); \ 34 myassert(!strcmp(buf, "OK\n"), n, "Invalid response from " msg ": %s%s", ERRRESP(buf)); \ 35 } while(0) 36 37 int main(int argc, char**argv) 38 { 39 myassert(argc == 3, 1, "Usage: %s <serialdev> <firmware.bfl>\n", argv[0]); 40 setbuf(stdout, NULL); 41 42 // Check filename 43 char *FWname = basename(strdup(argv[2])); 44 size_t FWnameLen = strlen(FWname); 45 myassert(FWnameLen <= 255, 0x0f, "Firmware filename '%s' is too long\n", FWname); 46 uint8_t n8 = FWnameLen; 47 48 // Open and check firmware file 49 FILE *FW = fopen(argv[2], "r"); 50 myassert(FW, 0x10, "Failed to open '%s' for reading\n", argv[2]); 51 char buf[0x20]; 52 myassert(1 == fread(buf, 7, 1, FW), 0x10, "Failed to read from '%s'\n", argv[2]); 53 myassert(!memcmp(buf, BFL_FILE_MAGIC, sizeof(BFL_FILE_MAGIC)-1), 0x11, "'%s' doesn't look like a BFL firmware\n", argv[2]); 54 myassert(!fseek(FW, 0, SEEK_END), 0x12, "Failed to find end of '%s'\n", argv[2]); 55 long FWlen = ftell(FW); 56 myassert(FWlen > 0, 0x12, "Couldn't get size of '%s'\n", argv[2]); 57 myassert(!fseek(FW, 7, SEEK_SET), 0x12, "Failed to rewind firmware file after getting size\n"); 58 FWlen -= 7; 59 printf("Firmware file looks OK :)\n"); 60 61 // Open device 62 FILE *BFL = fopen(argv[1], "r+"); 63 myassert(BFL, 0x20, "Failed to open '%s' for read/write\n", argv[1]); 64 myassert(!setvbuf(BFL, NULL, _IOFBF, 1032), 0x21, "Failed to setup buffer for device"); 65 66 // ZAX: Start firmware upload 67 printf("Starting firmware upload... "); 68 myassert(1 == fwrite("ZAX", 3, 1, BFL), 0x22, "Failed to issue ZAX command\n"); 69 WAITFOROK(0x22, "ZAX"); 70 71 // Firmware upload header 72 myassert(1 == fwrite(BFL_UPLOAD_MAGIC, sizeof(BFL_UPLOAD_MAGIC)-1, 1, BFL), 0x23, "Failed to send firmware upload header (magic)\n"); 73 uint32_t n32 = htonl(FWlen - FWlen / 6); 74 myassert(1 == fwrite(&n32, sizeof(n32), 1, BFL), 0x23, "Failed to send firmware upload header (size)\n"); 75 myassert(1 == fwrite("\0\0", 2 , 1, BFL), 0x23, "Failed to send firmware upload header (padding 1)\n"); 76 myassert(1 == fwrite(&n8, sizeof(n8) , 1, BFL), 0x23, "Failed to send firmware upload header (filename length)\n"); 77 myassert(1 == fwrite(FWname, n8 , 1, BFL), 0x23, "Failed to send firmware upload header (filename)\n"); 78 myassert(1 == fwrite("\0>>>>>>>>", 9 , 1, BFL), 0x23, "Failed to send firmware upload header (padding 2)\n"); 79 WAITFOROK(0x23, "firmware upload header"); 80 printf("OK, sending...\n"); 81 82 // Actual firmware upload 83 long i, j; 84 for (i = 0, j = 0; i < FWlen; ++i) { 85 myassert(1 == fread(&n8, sizeof(n8), 1, FW), 0x30, "Error reading data from firmware file\n"); 86 if (5 == i % 6) 87 continue; 88 n8 ^= 0x2f; 89 myassert(1 == fwrite(&n8, sizeof(n8), 1, BFL), 0x31, "Error sending data to device\n"); 90 if (!(++j % 0x400)) { 91 myassert(1 == fwrite(">>>>>>>>", 8, 1, BFL), 0x32, "Error sending block-finish to device\n"); 92 printf("\r%5.2f%% complete", (double)i * 100. / (double)FWlen); 93 WAITFOROK(0x32, "block-finish"); 94 } 95 } 96 printf("\r100%% complete :)\n"); 97 myassert(1 == fwrite(">>>>>>>>", 8, 1, BFL), 0x3f, "Error sending upload-finished to device\n"); 98 myassert(fgets(buf, sizeof(buf), BFL), 0x3f, "Error reading response from upload-finished\n"); 99 myassert(!strcmp(buf, "DONE\n"), 0x3f, "Invalid response from upload-finished: %s%s", ERRRESP(buf)); 100 101 // ZBX: Finish programming 102 printf("Waiting for device... "); 103 myassert(1 == fwrite("ZBX", 3, 1, BFL), 0x40, "Failed to issue ZBX command\n"); 104 WAITFOROK(0x40, "ZBX"); 105 printf("All done! Try mining to test the flash succeeded.\n"); 106 107 return 0; 108 }