blob: 3d8296fc6b0ca81a71d9cd92544ffda2ea9e3ee7 [file] [log] [blame]
Mike Dunn3497d432013-06-18 11:08:51 -07001/*
2 * Copyright (C) 2013 Mike Dunn <mikedunn@newsguy.com>
3 *
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.
6 *
7 *
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:
11 *
12 * modprobe docg4 ignore_badblocks=1 reliable_mode=1
13 *
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.
18 *
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.
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <sys/stat.h>
26#include <fcntl.h>
27#include <string.h>
28#include <sys/types.h>
29#include <unistd.h>
30#include <errno.h>
31#include <mtd/mtd-user.h>
32#include "libmtd.h"
33
34#define RELIABLE_BLOCKSIZE 0x10000 /* block capacity in reliable mode */
35#define STANDARD_BLOCKSIZE 0x40000 /* block capacity in normal mode */
36#define PAGESIZE 512
37#define PAGES_PER_BLOCK 512
38#define OOBSIZE 7 /* available to user (16 total) */
39
40uint8_t ff_oob[OOBSIZE] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
41
42/* this is the magic number the IPL looks for (ASCII "BIPO") */
43uint8_t page0_oob[OOBSIZE] = {'B', 'I', 'P', 'O', 0xff, 0xff, 0xff};
44
45int main(int argc, char * const argv[])
46{
47 int devfd, datafd, num_blocks, block;
48 off_t file_size;
49 libmtd_t mtd_desc;
50 struct mtd_dev_info devinfo;
51 uint8_t *blockbuf;
52 char response[8];
53
54 if (argc != 3) {
55 printf("usage: %s <image file> <mtd dev node>\n", argv[0]);
56 return -EINVAL;
57 }
58
59 mtd_desc = libmtd_open();
60 if (mtd_desc == NULL) {
61 int errsv = errno;
62 fprintf(stderr, "can't initialize libmtd\n");
63 return -errsv;
64 }
65
66 /* open the spl image file and mtd device */
67 datafd = open(argv[1], O_RDONLY);
68 if (datafd == -1) {
69 int errsv = errno;
70 perror(argv[1]);
71 return -errsv;
72 }
73 devfd = open(argv[2], O_WRONLY);
74 if (devfd == -1) {
75 int errsv = errno;
76 perror(argv[2]);
77 return -errsv;
78 }
79 if (mtd_get_dev_info(mtd_desc, argv[2], &devinfo) < 0) {
80 int errsv = errno;
81 perror(argv[2]);
82 return -errsv;
83 }
84
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) {
88 int errsv = errno;
89 perror("lseek");
90 return -errsv;
91 }
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) {
95 int errsv = errno;
96 perror("lseek");
97 return -errsv;
98 }
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");
103 return -EINVAL;
104 }
105
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')) {
110 printf("Exiting\n");
111 close(devfd);
112 close(datafd);
113 return 0;
114 }
115
116 blockbuf = calloc(RELIABLE_BLOCKSIZE, 1);
117 if (blockbuf == NULL) {
118 int errsv = errno;
119 perror("calloc");
120 return -errsv;
121 }
122
123 for (block = 0; block < num_blocks; block++) {
124 int ofs, page;
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;
128 int ret;
129
130 /* read data for one block from file */
131 while (len) {
132 ssize_t read_ret = read(datafd, buf, len);
133 if (read_ret == -1) {
134 int errsv = errno;
135 if (errno == EINTR)
136 continue;
137 perror("read");
138 return -errsv;
139 } else if (read_ret == 0) {
140 break; /* EOF */
141 }
142 len -= read_ret;
143 buf += read_ret;
144 }
145
146 printf("Block %d: writing\r", block + 1);
147 fflush(stdout);
148
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 */
154
155 ret = mtd_write(mtd_desc, &devinfo, devfd, block, ofs,
156 pagebuf, PAGESIZE, oobbuf, OOBSIZE,
157 MTD_OPS_PLACE_OOB);
158 if (ret) {
159 fprintf(stderr,
160 "\nmtd_write returned %d on block %d, ofs %x\n",
161 ret, block + 1, ofs);
162 return -EIO;
163 }
164 oobbuf = ff_oob; /* oob for subsequent pages */
165
166 if (page & 0x01) /* odd-numbered subpage */
167 pagebuf += PAGESIZE;
168 }
169 }
170
171 printf("\nDone\n");
172
173 close(devfd);
174 close(datafd);
175 free(blockbuf);
176 return 0;
177}