2 * Allwinner NAND randomizer and image builder implementation:
4 * Copyright © 2016 NextThing Co.
5 * Copyright © 2016 Free Electrons
11 #include <linux/bch.h>
16 #define BCH_PRIMITIVE_POLY 0x5803
18 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
19 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
35 static void swap_bits(uint8_t *buf, int len)
39 for (j = 0; j < len; j++) {
40 uint8_t byte = buf[j];
43 for (i = 0; i < 8; i++) {
45 buf[j] |= (1 << (7 - i));
50 static uint16_t lfsr_step(uint16_t state, int count)
54 state = ((state >> 1) |
55 ((((state >> 0) ^ (state >> 1)) & 1) << 14)) & 0x7fff;
60 static uint16_t default_scrambler_seeds[] = {
61 0x2b75, 0x0bd0, 0x5ca3, 0x62d1, 0x1c93, 0x07e9, 0x2162, 0x3a72,
62 0x0d67, 0x67f9, 0x1be7, 0x077d, 0x032f, 0x0dac, 0x2716, 0x2436,
63 0x7922, 0x1510, 0x3860, 0x5287, 0x480f, 0x4252, 0x1789, 0x5a2d,
64 0x2a49, 0x5e10, 0x437f, 0x4b4e, 0x2f45, 0x216e, 0x5cb7, 0x7130,
65 0x2a3f, 0x60e4, 0x4dc9, 0x0ef0, 0x0f52, 0x1bb9, 0x6211, 0x7a56,
66 0x226d, 0x4ea7, 0x6f36, 0x3692, 0x38bf, 0x0c62, 0x05eb, 0x4c55,
67 0x60f4, 0x728c, 0x3b6f, 0x2037, 0x7f69, 0x0936, 0x651a, 0x4ceb,
68 0x6218, 0x79f3, 0x383f, 0x18d9, 0x4f05, 0x5c82, 0x2912, 0x6f17,
69 0x6856, 0x5938, 0x1007, 0x61ab, 0x3e7f, 0x57c2, 0x542f, 0x4f62,
70 0x7454, 0x2eac, 0x7739, 0x42d4, 0x2f90, 0x435a, 0x2e52, 0x2064,
71 0x637c, 0x66ad, 0x2c90, 0x0bad, 0x759c, 0x0029, 0x0986, 0x7126,
72 0x1ca7, 0x1605, 0x386a, 0x27f5, 0x1380, 0x6d75, 0x24c3, 0x0f8e,
73 0x2b7a, 0x1418, 0x1fd1, 0x7dc1, 0x2d8e, 0x43af, 0x2267, 0x7da3,
74 0x4e3d, 0x1338, 0x50db, 0x454d, 0x764d, 0x40a3, 0x42e6, 0x262b,
75 0x2d2e, 0x1aea, 0x2e17, 0x173d, 0x3a6e, 0x71bf, 0x25f9, 0x0a5d,
76 0x7c57, 0x0fbe, 0x46ce, 0x4939, 0x6b17, 0x37bb, 0x3e91, 0x76db,
79 static uint16_t brom_scrambler_seeds[] = { 0x4a80 };
81 static void scramble(const struct image_info *info,
82 int page, uint8_t *data, int datalen)
87 /* Boot0 is always scrambled no matter the command line option. */
89 state = brom_scrambler_seeds[0];
91 unsigned seedmod = info->eraseblock_size / info->page_size;
93 /* Bail out earlier if the user didn't ask for scrambling. */
97 if (seedmod > ARRAY_SIZE(default_scrambler_seeds))
98 seedmod = ARRAY_SIZE(default_scrambler_seeds);
100 state = default_scrambler_seeds[page % seedmod];
103 /* Prepare the initial state... */
104 state = lfsr_step(state, 15);
106 /* and start scrambling data. */
107 for (i = 0; i < datalen; i++) {
109 state = lfsr_step(state, 8);
113 static int write_page(const struct image_info *info, uint8_t *buffer,
114 FILE *src, FILE *rnd, FILE *dst,
115 struct bch_control *bch, int page)
117 int steps = info->usable_page_size / info->ecc_step_size;
118 int eccbytes = DIV_ROUND_UP(info->ecc_strength * 14, 8);
119 off_t pos = ftell(dst);
126 memset(buffer, 0xff, info->page_size + info->oob_size);
127 cnt = fread(buffer, 1, info->usable_page_size, src);
131 "Failed to read data from the source\n");
138 fwrite(buffer, info->page_size + info->oob_size, 1, dst);
140 for (i = 0; i < info->usable_page_size; i++) {
141 if (buffer[i] != 0xff)
145 /* We leave empty pages at 0xff. */
146 if (i == info->usable_page_size)
149 /* Restore the source pointer to read it again. */
150 fseek(src, -cnt, SEEK_CUR);
152 /* Randomize unused space if scrambling is required. */
153 if (info->scramble) {
159 offs = steps * (info->ecc_step_size + eccbytes + 4);
160 cnt = info->page_size + info->oob_size - offs;
161 ret = fread(buffer + offs, 1, cnt, rnd);
162 if (!ret && !feof(rnd)) {
164 "Failed to read random data\n");
168 offs = info->page_size + (steps * (eccbytes + 4));
169 cnt = info->page_size + info->oob_size - offs;
170 memset(buffer + offs, 0xff, cnt);
171 scramble(info, page, buffer + offs, cnt);
173 fseek(dst, pos + offs, SEEK_SET);
174 fwrite(buffer + offs, cnt, 1, dst);
177 for (i = 0; i < steps; i++) {
178 int ecc_offs, data_offs;
181 memset(buffer, 0xff, info->ecc_step_size + eccbytes + 4);
182 ecc = buffer + info->ecc_step_size + 4;
184 data_offs = i * (info->ecc_step_size + eccbytes + 4);
185 ecc_offs = data_offs + info->ecc_step_size + 4;
187 data_offs = i * info->ecc_step_size;
188 ecc_offs = info->page_size + 4 + (i * (eccbytes + 4));
191 cnt = fread(buffer, 1, info->ecc_step_size, src);
192 if (!cnt && !feof(src)) {
194 "Failed to read data from the source\n");
198 pad = info->ecc_step_size - cnt;
200 if (info->scramble && info->boot0) {
203 ret = fread(buffer + cnt, 1, pad, rnd);
204 if (!ret && !feof(rnd)) {
206 "Failed to read random data\n");
210 memset(buffer + cnt, 0xff, pad);
214 memset(ecc, 0, eccbytes);
215 swap_bits(buffer, info->ecc_step_size + 4);
216 encode_bch(bch, buffer, info->ecc_step_size + 4, ecc);
217 swap_bits(buffer, info->ecc_step_size + 4);
218 swap_bits(ecc, eccbytes);
219 scramble(info, page, buffer, info->ecc_step_size + 4 + eccbytes);
221 fseek(dst, pos + data_offs, SEEK_SET);
222 fwrite(buffer, info->ecc_step_size, 1, dst);
223 fseek(dst, pos + ecc_offs - 4, SEEK_SET);
224 fwrite(ecc - 4, eccbytes + 4, 1, dst);
228 fseek(dst, pos + info->page_size, SEEK_SET);
229 memset(buffer, 0xff, 2);
230 fwrite(buffer, 2, 1, dst);
232 /* Make dst pointer point to the next page. */
233 fseek(dst, pos + info->page_size + info->oob_size, SEEK_SET);
238 static int create_image(const struct image_info *info)
240 off_t page = info->offset / info->page_size;
241 struct bch_control *bch;
242 FILE *src, *dst, *rnd;
245 bch = init_bch(14, info->ecc_strength, BCH_PRIMITIVE_POLY);
247 fprintf(stderr, "Failed to init the BCH engine\n");
251 buffer = malloc(info->page_size + info->oob_size);
253 fprintf(stderr, "Failed to allocate the NAND page buffer\n");
257 memset(buffer, 0xff, info->page_size + info->oob_size);
259 src = fopen(info->source, "r");
261 fprintf(stderr, "Failed to open source file (%s)\n",
266 dst = fopen(info->dest, "w");
268 fprintf(stderr, "Failed to open dest file (%s)\n", info->dest);
272 rnd = fopen("/dev/urandom", "r");
274 fprintf(stderr, "Failed to open /dev/urandom\n");
281 ret = write_page(info, buffer, src, rnd, dst, bch, page++);
289 static void display_help(int status)
291 fprintf(status == EXIT_SUCCESS ? stdout : stderr,
292 "sunxi-nand-image-builder %s\n"
294 "Usage: sunxi-nand-image-builder [OPTIONS] source-image output-image\n"
296 "Creates a raw NAND image that can be read by the sunxi NAND controller.\n"
298 "-h --help Display this help and exit\n"
299 "-c <str>/<step> --ecc=<str>/<step> ECC config (strength/step-size)\n"
300 "-p <size> --page=<size> Page size\n"
301 "-o <size> --oob=<size> OOB size\n"
302 "-u <size> --usable=<size> Usable page size\n"
303 "-e <size> --eraseblock=<size> Erase block size\n"
304 "-b --boot0 Build a boot0 image.\n"
305 "-s --scramble Scramble data\n"
306 "-a <offset> --address=<offset> Where the image will be programmed.\n"
309 "All the information you need to pass to this tool should be part of\n"
310 "the NAND datasheet.\n"
312 "The NAND controller only supports the following ECC configs\n"
313 " Valid ECC strengths: 16, 24, 28, 32, 40, 48, 56, 60 and 64\n"
314 " Valid ECC step size: 512 and 1024\n"
316 "If you are building a boot0 image, you'll have specify extra options.\n"
317 "These options should be chosen based on the layouts described here:\n"
318 " http://linux-sunxi.org/NAND#More_information_on_BROM_NAND\n"
320 " --usable should be assigned the 'Hardware page' value\n"
321 " --ecc should be assigned the 'ECC capacity'/'ECC page' values\n"
322 " --usable should be smaller than --page\n"
324 "The --address option is only required for non-boot0 images that are \n"
325 "meant to be programmed at a non eraseblock aligned offset.\n"
328 " The H27UCG8T2BTR-BC NAND exposes\n"
330 " * 1280 OOB bytes per page\n"
331 " * 4M eraseblocks\n"
332 " * requires data scrambling\n"
333 " * expects a minimum ECC of 40bits/1024bytes\n"
335 " A normal image can be generated with\n"
336 " sunxi-nand-image-builder -p 16384 -o 1280 -e 0x400000 -s -c 40/1024\n"
337 " A boot0 image can be generated with\n"
338 " sunxi-nand-image-builder -p 16384 -o 1280 -e 0x400000 -s -b -u 4096 -c 64/1024\n",
343 static int check_image_info(struct image_info *info)
345 static int valid_ecc_strengths[] = { 16, 24, 28, 32, 40, 48, 56, 60, 64 };
346 int eccbytes, eccsteps;
349 if (!info->page_size) {
350 fprintf(stderr, "--page is missing\n");
354 if (!info->page_size) {
355 fprintf(stderr, "--oob is missing\n");
359 if (!info->eraseblock_size) {
360 fprintf(stderr, "--eraseblock is missing\n");
364 if (info->ecc_step_size != 512 && info->ecc_step_size != 1024) {
365 fprintf(stderr, "Invalid ECC step argument: %d\n",
366 info->ecc_step_size);
370 for (i = 0; i < ARRAY_SIZE(valid_ecc_strengths); i++) {
371 if (valid_ecc_strengths[i] == info->ecc_strength)
375 if (i == ARRAY_SIZE(valid_ecc_strengths)) {
376 fprintf(stderr, "Invalid ECC strength argument: %d\n",
381 eccbytes = DIV_ROUND_UP(info->ecc_strength * 14, 8);
386 eccsteps = info->usable_page_size / info->ecc_step_size;
388 if (info->page_size + info->oob_size <
389 info->usable_page_size + (eccsteps * eccbytes)) {
391 "ECC bytes do not fit in the NAND page, choose a weaker ECC\n");
398 int main(int argc, char **argv)
400 struct image_info info;
402 memset(&info, 0, sizeof(info));
404 * Process user arguments
407 int option_index = 0;
409 static const struct option long_options[] = {
410 {"help", no_argument, 0, 'h'},
411 {"ecc", required_argument, 0, 'c'},
412 {"page", required_argument, 0, 'p'},
413 {"oob", required_argument, 0, 'o'},
414 {"usable", required_argument, 0, 'u'},
415 {"eraseblock", required_argument, 0, 'e'},
416 {"boot0", no_argument, 0, 'b'},
417 {"scramble", no_argument, 0, 's'},
418 {"address", required_argument, 0, 'a'},
422 int c = getopt_long(argc, argv, "c:p:o:u:e:ba:sh",
423 long_options, &option_index);
435 info.ecc_strength = strtol(optarg, &endptr, 0);
437 info.ecc_step_size = strtol(endptr + 1, NULL, 0);
440 info.page_size = strtol(optarg, NULL, 0);
443 info.oob_size = strtol(optarg, NULL, 0);
446 info.usable_page_size = strtol(optarg, NULL, 0);
449 info.eraseblock_size = strtol(optarg, NULL, 0);
455 info.offset = strtoull(optarg, NULL, 0);
463 if ((argc - optind) != 2)
466 info.source = argv[optind];
467 info.dest = argv[optind + 1];
470 info.usable_page_size = info.page_size;
471 } else if (!info.usable_page_size) {
472 if (info.page_size > 8192)
473 info.usable_page_size = 8192;
474 else if (info.page_size > 4096)
475 info.usable_page_size = 4096;
477 info.usable_page_size = 1024;
480 if (check_image_info(&info))
483 return create_image(&info);