Fork of the vendor (Boundary Devices) u-boot for Reform 2, with minor tweaks. The goal is to migrate to mainstream u-boot or barebox ASAP. The main impediment so far is the 4GB RAM config.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 
 
 

485 lines
12 KiB

  1. /*
  2. * Allwinner NAND randomizer and image builder implementation:
  3. *
  4. * Copyright © 2016 NextThing Co.
  5. * Copyright © 2016 Free Electrons
  6. *
  7. * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
  8. *
  9. */
  10. #include <linux/bch.h>
  11. #include <getopt.h>
  12. #include <version.h>
  13. #define BCH_PRIMITIVE_POLY 0x5803
  14. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  15. #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
  16. struct image_info {
  17. int ecc_strength;
  18. int ecc_step_size;
  19. int page_size;
  20. int oob_size;
  21. int usable_page_size;
  22. int eraseblock_size;
  23. int scramble;
  24. int boot0;
  25. off_t offset;
  26. const char *source;
  27. const char *dest;
  28. };
  29. static void swap_bits(uint8_t *buf, int len)
  30. {
  31. int i, j;
  32. for (j = 0; j < len; j++) {
  33. uint8_t byte = buf[j];
  34. buf[j] = 0;
  35. for (i = 0; i < 8; i++) {
  36. if (byte & (1 << i))
  37. buf[j] |= (1 << (7 - i));
  38. }
  39. }
  40. }
  41. static uint16_t lfsr_step(uint16_t state, int count)
  42. {
  43. state &= 0x7fff;
  44. while (count--)
  45. state = ((state >> 1) |
  46. ((((state >> 0) ^ (state >> 1)) & 1) << 14)) & 0x7fff;
  47. return state;
  48. }
  49. static uint16_t default_scrambler_seeds[] = {
  50. 0x2b75, 0x0bd0, 0x5ca3, 0x62d1, 0x1c93, 0x07e9, 0x2162, 0x3a72,
  51. 0x0d67, 0x67f9, 0x1be7, 0x077d, 0x032f, 0x0dac, 0x2716, 0x2436,
  52. 0x7922, 0x1510, 0x3860, 0x5287, 0x480f, 0x4252, 0x1789, 0x5a2d,
  53. 0x2a49, 0x5e10, 0x437f, 0x4b4e, 0x2f45, 0x216e, 0x5cb7, 0x7130,
  54. 0x2a3f, 0x60e4, 0x4dc9, 0x0ef0, 0x0f52, 0x1bb9, 0x6211, 0x7a56,
  55. 0x226d, 0x4ea7, 0x6f36, 0x3692, 0x38bf, 0x0c62, 0x05eb, 0x4c55,
  56. 0x60f4, 0x728c, 0x3b6f, 0x2037, 0x7f69, 0x0936, 0x651a, 0x4ceb,
  57. 0x6218, 0x79f3, 0x383f, 0x18d9, 0x4f05, 0x5c82, 0x2912, 0x6f17,
  58. 0x6856, 0x5938, 0x1007, 0x61ab, 0x3e7f, 0x57c2, 0x542f, 0x4f62,
  59. 0x7454, 0x2eac, 0x7739, 0x42d4, 0x2f90, 0x435a, 0x2e52, 0x2064,
  60. 0x637c, 0x66ad, 0x2c90, 0x0bad, 0x759c, 0x0029, 0x0986, 0x7126,
  61. 0x1ca7, 0x1605, 0x386a, 0x27f5, 0x1380, 0x6d75, 0x24c3, 0x0f8e,
  62. 0x2b7a, 0x1418, 0x1fd1, 0x7dc1, 0x2d8e, 0x43af, 0x2267, 0x7da3,
  63. 0x4e3d, 0x1338, 0x50db, 0x454d, 0x764d, 0x40a3, 0x42e6, 0x262b,
  64. 0x2d2e, 0x1aea, 0x2e17, 0x173d, 0x3a6e, 0x71bf, 0x25f9, 0x0a5d,
  65. 0x7c57, 0x0fbe, 0x46ce, 0x4939, 0x6b17, 0x37bb, 0x3e91, 0x76db,
  66. };
  67. static uint16_t brom_scrambler_seeds[] = { 0x4a80 };
  68. static void scramble(const struct image_info *info,
  69. int page, uint8_t *data, int datalen)
  70. {
  71. uint16_t state;
  72. int i;
  73. /* Boot0 is always scrambled no matter the command line option. */
  74. if (info->boot0) {
  75. state = brom_scrambler_seeds[0];
  76. } else {
  77. unsigned seedmod = info->eraseblock_size / info->page_size;
  78. /* Bail out earlier if the user didn't ask for scrambling. */
  79. if (!info->scramble)
  80. return;
  81. if (seedmod > ARRAY_SIZE(default_scrambler_seeds))
  82. seedmod = ARRAY_SIZE(default_scrambler_seeds);
  83. state = default_scrambler_seeds[page % seedmod];
  84. }
  85. /* Prepare the initial state... */
  86. state = lfsr_step(state, 15);
  87. /* and start scrambling data. */
  88. for (i = 0; i < datalen; i++) {
  89. data[i] ^= state;
  90. state = lfsr_step(state, 8);
  91. }
  92. }
  93. static int write_page(const struct image_info *info, uint8_t *buffer,
  94. FILE *src, FILE *rnd, FILE *dst,
  95. struct bch_control *bch, int page)
  96. {
  97. int steps = info->usable_page_size / info->ecc_step_size;
  98. int eccbytes = DIV_ROUND_UP(info->ecc_strength * 14, 8);
  99. off_t pos = ftell(dst);
  100. size_t pad, cnt;
  101. int i;
  102. if (eccbytes % 2)
  103. eccbytes++;
  104. memset(buffer, 0xff, info->page_size + info->oob_size);
  105. cnt = fread(buffer, 1, info->usable_page_size, src);
  106. if (!cnt) {
  107. if (!feof(src)) {
  108. fprintf(stderr,
  109. "Failed to read data from the source\n");
  110. return -1;
  111. } else {
  112. return 0;
  113. }
  114. }
  115. fwrite(buffer, info->page_size + info->oob_size, 1, dst);
  116. for (i = 0; i < info->usable_page_size; i++) {
  117. if (buffer[i] != 0xff)
  118. break;
  119. }
  120. /* We leave empty pages at 0xff. */
  121. if (i == info->usable_page_size)
  122. return 0;
  123. /* Restore the source pointer to read it again. */
  124. fseek(src, -cnt, SEEK_CUR);
  125. /* Randomize unused space if scrambling is required. */
  126. if (info->scramble) {
  127. int offs;
  128. if (info->boot0) {
  129. size_t ret;
  130. offs = steps * (info->ecc_step_size + eccbytes + 4);
  131. cnt = info->page_size + info->oob_size - offs;
  132. ret = fread(buffer + offs, 1, cnt, rnd);
  133. if (!ret && !feof(rnd)) {
  134. fprintf(stderr,
  135. "Failed to read random data\n");
  136. return -1;
  137. }
  138. } else {
  139. offs = info->page_size + (steps * (eccbytes + 4));
  140. cnt = info->page_size + info->oob_size - offs;
  141. memset(buffer + offs, 0xff, cnt);
  142. scramble(info, page, buffer + offs, cnt);
  143. }
  144. fseek(dst, pos + offs, SEEK_SET);
  145. fwrite(buffer + offs, cnt, 1, dst);
  146. }
  147. for (i = 0; i < steps; i++) {
  148. int ecc_offs, data_offs;
  149. uint8_t *ecc;
  150. memset(buffer, 0xff, info->ecc_step_size + eccbytes + 4);
  151. ecc = buffer + info->ecc_step_size + 4;
  152. if (info->boot0) {
  153. data_offs = i * (info->ecc_step_size + eccbytes + 4);
  154. ecc_offs = data_offs + info->ecc_step_size + 4;
  155. } else {
  156. data_offs = i * info->ecc_step_size;
  157. ecc_offs = info->page_size + 4 + (i * (eccbytes + 4));
  158. }
  159. cnt = fread(buffer, 1, info->ecc_step_size, src);
  160. if (!cnt && !feof(src)) {
  161. fprintf(stderr,
  162. "Failed to read data from the source\n");
  163. return -1;
  164. }
  165. pad = info->ecc_step_size - cnt;
  166. if (pad) {
  167. if (info->scramble && info->boot0) {
  168. size_t ret;
  169. ret = fread(buffer + cnt, 1, pad, rnd);
  170. if (!ret && !feof(rnd)) {
  171. fprintf(stderr,
  172. "Failed to read random data\n");
  173. return -1;
  174. }
  175. } else {
  176. memset(buffer + cnt, 0xff, pad);
  177. }
  178. }
  179. memset(ecc, 0, eccbytes);
  180. swap_bits(buffer, info->ecc_step_size + 4);
  181. encode_bch(bch, buffer, info->ecc_step_size + 4, ecc);
  182. swap_bits(buffer, info->ecc_step_size + 4);
  183. swap_bits(ecc, eccbytes);
  184. scramble(info, page, buffer, info->ecc_step_size + 4 + eccbytes);
  185. fseek(dst, pos + data_offs, SEEK_SET);
  186. fwrite(buffer, info->ecc_step_size, 1, dst);
  187. fseek(dst, pos + ecc_offs - 4, SEEK_SET);
  188. fwrite(ecc - 4, eccbytes + 4, 1, dst);
  189. }
  190. /* Fix BBM. */
  191. fseek(dst, pos + info->page_size, SEEK_SET);
  192. memset(buffer, 0xff, 2);
  193. fwrite(buffer, 2, 1, dst);
  194. /* Make dst pointer point to the next page. */
  195. fseek(dst, pos + info->page_size + info->oob_size, SEEK_SET);
  196. return 0;
  197. }
  198. static int create_image(const struct image_info *info)
  199. {
  200. off_t page = info->offset / info->page_size;
  201. struct bch_control *bch;
  202. FILE *src, *dst, *rnd;
  203. uint8_t *buffer;
  204. bch = init_bch(14, info->ecc_strength, BCH_PRIMITIVE_POLY);
  205. if (!bch) {
  206. fprintf(stderr, "Failed to init the BCH engine\n");
  207. return -1;
  208. }
  209. buffer = malloc(info->page_size + info->oob_size);
  210. if (!buffer) {
  211. fprintf(stderr, "Failed to allocate the NAND page buffer\n");
  212. return -1;
  213. }
  214. memset(buffer, 0xff, info->page_size + info->oob_size);
  215. src = fopen(info->source, "r");
  216. if (!src) {
  217. fprintf(stderr, "Failed to open source file (%s)\n",
  218. info->source);
  219. return -1;
  220. }
  221. dst = fopen(info->dest, "w");
  222. if (!dst) {
  223. fprintf(stderr, "Failed to open dest file (%s)\n", info->dest);
  224. return -1;
  225. }
  226. rnd = fopen("/dev/urandom", "r");
  227. if (!rnd) {
  228. fprintf(stderr, "Failed to open /dev/urandom\n");
  229. return -1;
  230. }
  231. while (!feof(src)) {
  232. int ret;
  233. ret = write_page(info, buffer, src, rnd, dst, bch, page++);
  234. if (ret)
  235. return ret;
  236. }
  237. return 0;
  238. }
  239. static void display_help(int status)
  240. {
  241. fprintf(status == EXIT_SUCCESS ? stdout : stderr,
  242. "sunxi-nand-image-builder %s\n"
  243. "\n"
  244. "Usage: sunxi-nand-image-builder [OPTIONS] source-image output-image\n"
  245. "\n"
  246. "Creates a raw NAND image that can be read by the sunxi NAND controller.\n"
  247. "\n"
  248. "-h --help Display this help and exit\n"
  249. "-c <str>/<step> --ecc=<str>/<step> ECC config (strength/step-size)\n"
  250. "-p <size> --page=<size> Page size\n"
  251. "-o <size> --oob=<size> OOB size\n"
  252. "-u <size> --usable=<size> Usable page size\n"
  253. "-e <size> --eraseblock=<size> Erase block size\n"
  254. "-b --boot0 Build a boot0 image.\n"
  255. "-s --scramble Scramble data\n"
  256. "-a <offset> --address=<offset> Where the image will be programmed.\n"
  257. "\n"
  258. "Notes:\n"
  259. "All the information you need to pass to this tool should be part of\n"
  260. "the NAND datasheet.\n"
  261. "\n"
  262. "The NAND controller only supports the following ECC configs\n"
  263. " Valid ECC strengths: 16, 24, 28, 32, 40, 48, 56, 60 and 64\n"
  264. " Valid ECC step size: 512 and 1024\n"
  265. "\n"
  266. "If you are building a boot0 image, you'll have specify extra options.\n"
  267. "These options should be chosen based on the layouts described here:\n"
  268. " http://linux-sunxi.org/NAND#More_information_on_BROM_NAND\n"
  269. "\n"
  270. " --usable should be assigned the 'Hardware page' value\n"
  271. " --ecc should be assigned the 'ECC capacity'/'ECC page' values\n"
  272. " --usable should be smaller than --page\n"
  273. "\n"
  274. "The --address option is only required for non-boot0 images that are \n"
  275. "meant to be programmed at a non eraseblock aligned offset.\n"
  276. "\n"
  277. "Examples:\n"
  278. " The H27UCG8T2BTR-BC NAND exposes\n"
  279. " * 16k pages\n"
  280. " * 1280 OOB bytes per page\n"
  281. " * 4M eraseblocks\n"
  282. " * requires data scrambling\n"
  283. " * expects a minimum ECC of 40bits/1024bytes\n"
  284. "\n"
  285. " A normal image can be generated with\n"
  286. " sunxi-nand-image-builder -p 16384 -o 1280 -e 0x400000 -s -c 40/1024\n"
  287. " A boot0 image can be generated with\n"
  288. " sunxi-nand-image-builder -p 16384 -o 1280 -e 0x400000 -s -b -u 4096 -c 64/1024\n",
  289. PLAIN_VERSION);
  290. exit(status);
  291. }
  292. static int check_image_info(struct image_info *info)
  293. {
  294. static int valid_ecc_strengths[] = { 16, 24, 28, 32, 40, 48, 56, 60, 64 };
  295. int eccbytes, eccsteps;
  296. unsigned i;
  297. if (!info->page_size) {
  298. fprintf(stderr, "--page is missing\n");
  299. return -EINVAL;
  300. }
  301. if (!info->page_size) {
  302. fprintf(stderr, "--oob is missing\n");
  303. return -EINVAL;
  304. }
  305. if (!info->eraseblock_size) {
  306. fprintf(stderr, "--eraseblock is missing\n");
  307. return -EINVAL;
  308. }
  309. if (info->ecc_step_size != 512 && info->ecc_step_size != 1024) {
  310. fprintf(stderr, "Invalid ECC step argument: %d\n",
  311. info->ecc_step_size);
  312. return -EINVAL;
  313. }
  314. for (i = 0; i < ARRAY_SIZE(valid_ecc_strengths); i++) {
  315. if (valid_ecc_strengths[i] == info->ecc_strength)
  316. break;
  317. }
  318. if (i == ARRAY_SIZE(valid_ecc_strengths)) {
  319. fprintf(stderr, "Invalid ECC strength argument: %d\n",
  320. info->ecc_strength);
  321. return -EINVAL;
  322. }
  323. eccbytes = DIV_ROUND_UP(info->ecc_strength * 14, 8);
  324. if (eccbytes % 2)
  325. eccbytes++;
  326. eccbytes += 4;
  327. eccsteps = info->usable_page_size / info->ecc_step_size;
  328. if (info->page_size + info->oob_size <
  329. info->usable_page_size + (eccsteps * eccbytes)) {
  330. fprintf(stderr,
  331. "ECC bytes do not fit in the NAND page, choose a weaker ECC\n");
  332. return -EINVAL;
  333. }
  334. return 0;
  335. }
  336. int main(int argc, char **argv)
  337. {
  338. struct image_info info;
  339. memset(&info, 0, sizeof(info));
  340. /*
  341. * Process user arguments
  342. */
  343. for (;;) {
  344. int option_index = 0;
  345. char *endptr = NULL;
  346. static const struct option long_options[] = {
  347. {"help", no_argument, 0, 'h'},
  348. {"ecc", required_argument, 0, 'c'},
  349. {"page", required_argument, 0, 'p'},
  350. {"oob", required_argument, 0, 'o'},
  351. {"usable", required_argument, 0, 'u'},
  352. {"eraseblock", required_argument, 0, 'e'},
  353. {"boot0", no_argument, 0, 'b'},
  354. {"scramble", no_argument, 0, 's'},
  355. {"address", required_argument, 0, 'a'},
  356. {0, 0, 0, 0},
  357. };
  358. int c = getopt_long(argc, argv, "c:p:o:u:e:ba:sh",
  359. long_options, &option_index);
  360. if (c == EOF)
  361. break;
  362. switch (c) {
  363. case 'h':
  364. display_help(0);
  365. break;
  366. case 's':
  367. info.scramble = 1;
  368. break;
  369. case 'c':
  370. info.ecc_strength = strtol(optarg, &endptr, 0);
  371. if (*endptr == '/')
  372. info.ecc_step_size = strtol(endptr + 1, NULL, 0);
  373. break;
  374. case 'p':
  375. info.page_size = strtol(optarg, NULL, 0);
  376. break;
  377. case 'o':
  378. info.oob_size = strtol(optarg, NULL, 0);
  379. break;
  380. case 'u':
  381. info.usable_page_size = strtol(optarg, NULL, 0);
  382. break;
  383. case 'e':
  384. info.eraseblock_size = strtol(optarg, NULL, 0);
  385. break;
  386. case 'b':
  387. info.boot0 = 1;
  388. break;
  389. case 'a':
  390. info.offset = strtoull(optarg, NULL, 0);
  391. break;
  392. case '?':
  393. display_help(-1);
  394. break;
  395. }
  396. }
  397. if ((argc - optind) != 2)
  398. display_help(-1);
  399. info.source = argv[optind];
  400. info.dest = argv[optind + 1];
  401. if (!info.boot0) {
  402. info.usable_page_size = info.page_size;
  403. } else if (!info.usable_page_size) {
  404. if (info.page_size > 8192)
  405. info.usable_page_size = 8192;
  406. else if (info.page_size > 4096)
  407. info.usable_page_size = 4096;
  408. else
  409. info.usable_page_size = 1024;
  410. }
  411. if (check_image_info(&info))
  412. display_help(-1);
  413. return create_image(&info);
  414. }