bitforce-firmware-flash.c 4.2 KB

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