2 * Copyright (C) 2013 Mike Dunn <mikedunn@newsguy.com>
4 * This file is released under the terms of GPL v2 and any later version.
5 * See the file COPYING in the root directory of the source tree for details.
8 * This is a userspace Linux utility that, when run on the Treo 680, will
9 * program u-boot to flash. The docg4 driver *must* be loaded with the
10 * reliable_mode and ignore_badblocks parameters enabled:
12 * modprobe docg4 ignore_badblocks=1 reliable_mode=1
14 * This utility writes the concatenated spl + u-boot image to the start of the
15 * mtd device in the format expected by the IPL/SPL. The image file and mtd
16 * device node are passed to the utility as arguments. The blocks must have
17 * been erased beforehand.
19 * When you compile this, note that it links to libmtd from mtd-utils, so ensure
20 * that your include and lib paths include this.
28 #include <sys/types.h>
31 #include <mtd/mtd-user.h>
34 #define RELIABLE_BLOCKSIZE 0x10000 /* block capacity in reliable mode */
35 #define STANDARD_BLOCKSIZE 0x40000 /* block capacity in normal mode */
37 #define PAGES_PER_BLOCK 512
38 #define OOBSIZE 7 /* available to user (16 total) */
40 uint8_t ff_oob[OOBSIZE] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
42 /* this is the magic number the IPL looks for (ASCII "BIPO") */
43 uint8_t page0_oob[OOBSIZE] = {'B', 'I', 'P', 'O', 0xff, 0xff, 0xff};
45 int main(int argc, char * const argv[])
47 int devfd, datafd, num_blocks, block;
50 struct mtd_dev_info devinfo;
55 printf("usage: %s <image file> <mtd dev node>\n", argv[0]);
59 mtd_desc = libmtd_open();
60 if (mtd_desc == NULL) {
62 fprintf(stderr, "can't initialize libmtd\n");
66 /* open the spl image file and mtd device */
67 datafd = open(argv[1], O_RDONLY);
73 devfd = open(argv[2], O_WRONLY);
79 if (mtd_get_dev_info(mtd_desc, argv[2], &devinfo) < 0) {
85 /* determine the number of blocks needed by the image */
86 file_size = lseek(datafd, 0, SEEK_END);
87 if (file_size == (off_t)-1) {
92 num_blocks = (file_size + RELIABLE_BLOCKSIZE - 1) / RELIABLE_BLOCKSIZE;
93 file_size = lseek(datafd, 0, SEEK_SET);
94 if (file_size == (off_t)-1) {
99 printf("The mtd partition contains %d blocks\n", devinfo.eb_cnt);
100 printf("U-boot will occupy %d blocks\n", num_blocks);
101 if (num_blocks > devinfo.eb_cnt) {
102 fprintf(stderr, "Insufficient blocks on partition\n");
106 printf("IMPORTANT: These blocks must be in an erased state!\n");
107 printf("Do you want to proceed?\n");
108 scanf("%s", response);
109 if ((response[0] != 'y') && (response[0] != 'Y')) {
116 blockbuf = calloc(RELIABLE_BLOCKSIZE, 1);
117 if (blockbuf == NULL) {
123 for (block = 0; block < num_blocks; block++) {
125 uint8_t *pagebuf = blockbuf, *buf = blockbuf;
126 uint8_t *oobbuf = page0_oob; /* magic num in oob of 1st page */
127 size_t len = RELIABLE_BLOCKSIZE;
130 /* read data for one block from file */
132 ssize_t read_ret = read(datafd, buf, len);
133 if (read_ret == -1) {
139 } else if (read_ret == 0) {
146 printf("Block %d: writing\r", block + 1);
149 for (page = 0, ofs = 0;
150 page < PAGES_PER_BLOCK;
151 page++, ofs += PAGESIZE) {
152 if (page & 0x04) /* Odd-numbered 2k page */
153 continue; /* skipped in reliable mode */
155 ret = mtd_write(mtd_desc, &devinfo, devfd, block, ofs,
156 pagebuf, PAGESIZE, oobbuf, OOBSIZE,
160 "\nmtd_write returned %d on block %d, ofs %x\n",
161 ret, block + 1, ofs);
164 oobbuf = ff_oob; /* oob for subsequent pages */
166 if (page & 0x01) /* odd-numbered subpage */