1312a10f1SAlex Kiernan // SPDX-License-Identifier: GPL-2.0+
2312a10f1SAlex Kiernan /*
3312a10f1SAlex Kiernan * Copyright 2014 Broadcom Corporation.
4312a10f1SAlex Kiernan * Copyright 2015 Free Electrons.
5312a10f1SAlex Kiernan */
6312a10f1SAlex Kiernan
7312a10f1SAlex Kiernan #include <config.h>
8312a10f1SAlex Kiernan #include <common.h>
9312a10f1SAlex Kiernan
10312a10f1SAlex Kiernan #include <fastboot.h>
11312a10f1SAlex Kiernan #include <image-sparse.h>
12312a10f1SAlex Kiernan
13312a10f1SAlex Kiernan #include <linux/mtd/mtd.h>
14312a10f1SAlex Kiernan #include <jffs2/jffs2.h>
15312a10f1SAlex Kiernan #include <nand.h>
16312a10f1SAlex Kiernan
17312a10f1SAlex Kiernan struct fb_nand_sparse {
18312a10f1SAlex Kiernan struct mtd_info *mtd;
19312a10f1SAlex Kiernan struct part_info *part;
20312a10f1SAlex Kiernan };
21312a10f1SAlex Kiernan
board_fastboot_erase_partition_setup(char * name)22312a10f1SAlex Kiernan __weak int board_fastboot_erase_partition_setup(char *name)
23312a10f1SAlex Kiernan {
24312a10f1SAlex Kiernan return 0;
25312a10f1SAlex Kiernan }
26312a10f1SAlex Kiernan
board_fastboot_write_partition_setup(char * name)27312a10f1SAlex Kiernan __weak int board_fastboot_write_partition_setup(char *name)
28312a10f1SAlex Kiernan {
29312a10f1SAlex Kiernan return 0;
30312a10f1SAlex Kiernan }
31312a10f1SAlex Kiernan
fb_nand_lookup(const char * partname,struct mtd_info ** mtd,struct part_info ** part,char * response)32312a10f1SAlex Kiernan static int fb_nand_lookup(const char *partname,
33312a10f1SAlex Kiernan struct mtd_info **mtd,
34c4ded03eSAlex Kiernan struct part_info **part,
35c4ded03eSAlex Kiernan char *response)
36312a10f1SAlex Kiernan {
37312a10f1SAlex Kiernan struct mtd_device *dev;
38312a10f1SAlex Kiernan int ret;
39312a10f1SAlex Kiernan u8 pnum;
40312a10f1SAlex Kiernan
41312a10f1SAlex Kiernan ret = mtdparts_init();
42312a10f1SAlex Kiernan if (ret) {
43312a10f1SAlex Kiernan pr_err("Cannot initialize MTD partitions\n");
44c4ded03eSAlex Kiernan fastboot_fail("cannot init mtdparts", response);
45312a10f1SAlex Kiernan return ret;
46312a10f1SAlex Kiernan }
47312a10f1SAlex Kiernan
48312a10f1SAlex Kiernan ret = find_dev_and_part(partname, &dev, &pnum, part);
49312a10f1SAlex Kiernan if (ret) {
50312a10f1SAlex Kiernan pr_err("cannot find partition: '%s'", partname);
51c4ded03eSAlex Kiernan fastboot_fail("cannot find partition", response);
52312a10f1SAlex Kiernan return ret;
53312a10f1SAlex Kiernan }
54312a10f1SAlex Kiernan
55312a10f1SAlex Kiernan if (dev->id->type != MTD_DEV_TYPE_NAND) {
56312a10f1SAlex Kiernan pr_err("partition '%s' is not stored on a NAND device",
57312a10f1SAlex Kiernan partname);
58c4ded03eSAlex Kiernan fastboot_fail("not a NAND device", response);
59312a10f1SAlex Kiernan return -EINVAL;
60312a10f1SAlex Kiernan }
61312a10f1SAlex Kiernan
62312a10f1SAlex Kiernan *mtd = get_nand_dev_by_index(dev->id->num);
63312a10f1SAlex Kiernan
64312a10f1SAlex Kiernan return 0;
65312a10f1SAlex Kiernan }
66312a10f1SAlex Kiernan
_fb_nand_erase(struct mtd_info * mtd,struct part_info * part)67312a10f1SAlex Kiernan static int _fb_nand_erase(struct mtd_info *mtd, struct part_info *part)
68312a10f1SAlex Kiernan {
69312a10f1SAlex Kiernan nand_erase_options_t opts;
70312a10f1SAlex Kiernan int ret;
71312a10f1SAlex Kiernan
72312a10f1SAlex Kiernan memset(&opts, 0, sizeof(opts));
73312a10f1SAlex Kiernan opts.offset = part->offset;
74312a10f1SAlex Kiernan opts.length = part->size;
75312a10f1SAlex Kiernan opts.quiet = 1;
76312a10f1SAlex Kiernan
77312a10f1SAlex Kiernan printf("Erasing blocks 0x%llx to 0x%llx\n",
78312a10f1SAlex Kiernan part->offset, part->offset + part->size);
79312a10f1SAlex Kiernan
80312a10f1SAlex Kiernan ret = nand_erase_opts(mtd, &opts);
81312a10f1SAlex Kiernan if (ret)
82312a10f1SAlex Kiernan return ret;
83312a10f1SAlex Kiernan
84312a10f1SAlex Kiernan printf("........ erased 0x%llx bytes from '%s'\n",
85312a10f1SAlex Kiernan part->size, part->name);
86312a10f1SAlex Kiernan
87312a10f1SAlex Kiernan return 0;
88312a10f1SAlex Kiernan }
89312a10f1SAlex Kiernan
_fb_nand_write(struct mtd_info * mtd,struct part_info * part,void * buffer,u32 offset,size_t length,size_t * written)90312a10f1SAlex Kiernan static int _fb_nand_write(struct mtd_info *mtd, struct part_info *part,
91*f73a7df9SAlex Kiernan void *buffer, u32 offset,
9252fdf107SAlex Kiernan size_t length, size_t *written)
93312a10f1SAlex Kiernan {
94312a10f1SAlex Kiernan int flags = WITH_WR_VERIFY;
95312a10f1SAlex Kiernan
96312a10f1SAlex Kiernan #ifdef CONFIG_FASTBOOT_FLASH_NAND_TRIMFFS
97312a10f1SAlex Kiernan flags |= WITH_DROP_FFS;
98312a10f1SAlex Kiernan #endif
99312a10f1SAlex Kiernan
100312a10f1SAlex Kiernan return nand_write_skip_bad(mtd, offset, &length, written,
101312a10f1SAlex Kiernan part->size - (offset - part->offset),
102312a10f1SAlex Kiernan buffer, flags);
103312a10f1SAlex Kiernan }
104312a10f1SAlex Kiernan
fb_nand_sparse_write(struct sparse_storage * info,lbaint_t blk,lbaint_t blkcnt,const void * buffer)105312a10f1SAlex Kiernan static lbaint_t fb_nand_sparse_write(struct sparse_storage *info,
106312a10f1SAlex Kiernan lbaint_t blk, lbaint_t blkcnt, const void *buffer)
107312a10f1SAlex Kiernan {
108312a10f1SAlex Kiernan struct fb_nand_sparse *sparse = info->priv;
109312a10f1SAlex Kiernan size_t written;
110312a10f1SAlex Kiernan int ret;
111312a10f1SAlex Kiernan
112312a10f1SAlex Kiernan ret = _fb_nand_write(sparse->mtd, sparse->part, (void *)buffer,
113312a10f1SAlex Kiernan blk * info->blksz,
114312a10f1SAlex Kiernan blkcnt * info->blksz, &written);
115312a10f1SAlex Kiernan if (ret < 0) {
116312a10f1SAlex Kiernan printf("Failed to write sparse chunk\n");
117312a10f1SAlex Kiernan return ret;
118312a10f1SAlex Kiernan }
119312a10f1SAlex Kiernan
120312a10f1SAlex Kiernan /* TODO - verify that the value "written" includes the "bad-blocks" ... */
121312a10f1SAlex Kiernan
122312a10f1SAlex Kiernan /*
123312a10f1SAlex Kiernan * the return value must be 'blkcnt' ("good-blocks") plus the
124312a10f1SAlex Kiernan * number of "bad-blocks" encountered within this space...
125312a10f1SAlex Kiernan */
126312a10f1SAlex Kiernan return written / info->blksz;
127312a10f1SAlex Kiernan }
128312a10f1SAlex Kiernan
fb_nand_sparse_reserve(struct sparse_storage * info,lbaint_t blk,lbaint_t blkcnt)129312a10f1SAlex Kiernan static lbaint_t fb_nand_sparse_reserve(struct sparse_storage *info,
130312a10f1SAlex Kiernan lbaint_t blk, lbaint_t blkcnt)
131312a10f1SAlex Kiernan {
132312a10f1SAlex Kiernan int bad_blocks = 0;
133312a10f1SAlex Kiernan
134312a10f1SAlex Kiernan /*
135312a10f1SAlex Kiernan * TODO - implement a function to determine the total number
136312a10f1SAlex Kiernan * of blocks which must be used in order to reserve the specified
137312a10f1SAlex Kiernan * number ("blkcnt") of "good-blocks", starting at "blk"...
138312a10f1SAlex Kiernan * ( possibly something like the "check_skip_len()" function )
139312a10f1SAlex Kiernan */
140312a10f1SAlex Kiernan
141312a10f1SAlex Kiernan /*
142312a10f1SAlex Kiernan * the return value must be 'blkcnt' ("good-blocks") plus the
143312a10f1SAlex Kiernan * number of "bad-blocks" encountered within this space...
144312a10f1SAlex Kiernan */
145312a10f1SAlex Kiernan return blkcnt + bad_blocks;
146312a10f1SAlex Kiernan }
147312a10f1SAlex Kiernan
148d1a119d4SAlex Kiernan /**
149*f73a7df9SAlex Kiernan * fastboot_nand_get_part_info() - Lookup NAND partion by name
150*f73a7df9SAlex Kiernan *
151*f73a7df9SAlex Kiernan * @part_name: Named device to lookup
152*f73a7df9SAlex Kiernan * @part_info: Pointer to returned part_info pointer
153*f73a7df9SAlex Kiernan * @response: Pointer to fastboot response buffer
154*f73a7df9SAlex Kiernan */
fastboot_nand_get_part_info(char * part_name,struct part_info ** part_info,char * response)155*f73a7df9SAlex Kiernan int fastboot_nand_get_part_info(char *part_name, struct part_info **part_info,
156*f73a7df9SAlex Kiernan char *response)
157*f73a7df9SAlex Kiernan {
158*f73a7df9SAlex Kiernan struct mtd_info *mtd = NULL;
159*f73a7df9SAlex Kiernan
160*f73a7df9SAlex Kiernan return fb_nand_lookup(part_name, &mtd, part_info, response);
161*f73a7df9SAlex Kiernan }
162*f73a7df9SAlex Kiernan
163*f73a7df9SAlex Kiernan /**
164d1a119d4SAlex Kiernan * fastboot_nand_flash_write() - Write image to NAND for fastboot
165d1a119d4SAlex Kiernan *
166d1a119d4SAlex Kiernan * @cmd: Named device to write image to
167d1a119d4SAlex Kiernan * @download_buffer: Pointer to image data
168d1a119d4SAlex Kiernan * @download_bytes: Size of image data
169d1a119d4SAlex Kiernan * @response: Pointer to fastboot response buffer
170d1a119d4SAlex Kiernan */
fastboot_nand_flash_write(const char * cmd,void * download_buffer,u32 download_bytes,char * response)171d1a119d4SAlex Kiernan void fastboot_nand_flash_write(const char *cmd, void *download_buffer,
172*f73a7df9SAlex Kiernan u32 download_bytes, char *response)
173312a10f1SAlex Kiernan {
174312a10f1SAlex Kiernan struct part_info *part;
175312a10f1SAlex Kiernan struct mtd_info *mtd = NULL;
176312a10f1SAlex Kiernan int ret;
177312a10f1SAlex Kiernan
178c4ded03eSAlex Kiernan ret = fb_nand_lookup(cmd, &mtd, &part, response);
179312a10f1SAlex Kiernan if (ret) {
180312a10f1SAlex Kiernan pr_err("invalid NAND device");
181c4ded03eSAlex Kiernan fastboot_fail("invalid NAND device", response);
182312a10f1SAlex Kiernan return;
183312a10f1SAlex Kiernan }
184312a10f1SAlex Kiernan
185312a10f1SAlex Kiernan ret = board_fastboot_write_partition_setup(part->name);
186312a10f1SAlex Kiernan if (ret)
187312a10f1SAlex Kiernan return;
188312a10f1SAlex Kiernan
189312a10f1SAlex Kiernan if (is_sparse_image(download_buffer)) {
190312a10f1SAlex Kiernan struct fb_nand_sparse sparse_priv;
191312a10f1SAlex Kiernan struct sparse_storage sparse;
192312a10f1SAlex Kiernan
193312a10f1SAlex Kiernan sparse_priv.mtd = mtd;
194312a10f1SAlex Kiernan sparse_priv.part = part;
195312a10f1SAlex Kiernan
196312a10f1SAlex Kiernan sparse.blksz = mtd->writesize;
197312a10f1SAlex Kiernan sparse.start = part->offset / sparse.blksz;
198312a10f1SAlex Kiernan sparse.size = part->size / sparse.blksz;
199312a10f1SAlex Kiernan sparse.write = fb_nand_sparse_write;
200312a10f1SAlex Kiernan sparse.reserve = fb_nand_sparse_reserve;
201312a10f1SAlex Kiernan sparse.mssg = fastboot_fail;
202312a10f1SAlex Kiernan
203312a10f1SAlex Kiernan printf("Flashing sparse image at offset " LBAFU "\n",
204312a10f1SAlex Kiernan sparse.start);
205312a10f1SAlex Kiernan
206312a10f1SAlex Kiernan sparse.priv = &sparse_priv;
207c4ded03eSAlex Kiernan ret = write_sparse_image(&sparse, cmd, download_buffer,
208c4ded03eSAlex Kiernan response);
209312a10f1SAlex Kiernan if (!ret)
210c4ded03eSAlex Kiernan fastboot_okay(NULL, response);
211312a10f1SAlex Kiernan } else {
212312a10f1SAlex Kiernan printf("Flashing raw image at offset 0x%llx\n",
213312a10f1SAlex Kiernan part->offset);
214312a10f1SAlex Kiernan
215312a10f1SAlex Kiernan ret = _fb_nand_write(mtd, part, download_buffer, part->offset,
216312a10f1SAlex Kiernan download_bytes, NULL);
217312a10f1SAlex Kiernan
218312a10f1SAlex Kiernan printf("........ wrote %u bytes to '%s'\n",
219312a10f1SAlex Kiernan download_bytes, part->name);
220312a10f1SAlex Kiernan }
221312a10f1SAlex Kiernan
222312a10f1SAlex Kiernan if (ret) {
223c4ded03eSAlex Kiernan fastboot_fail("error writing the image", response);
224312a10f1SAlex Kiernan return;
225312a10f1SAlex Kiernan }
226312a10f1SAlex Kiernan
227c4ded03eSAlex Kiernan fastboot_okay(NULL, response);
228312a10f1SAlex Kiernan }
229312a10f1SAlex Kiernan
230d1a119d4SAlex Kiernan /**
231d1a119d4SAlex Kiernan * fastboot_nand_flash_erase() - Erase NAND for fastboot
232d1a119d4SAlex Kiernan *
233d1a119d4SAlex Kiernan * @cmd: Named device to erase
234d1a119d4SAlex Kiernan * @response: Pointer to fastboot response buffer
235d1a119d4SAlex Kiernan */
fastboot_nand_erase(const char * cmd,char * response)236d1a119d4SAlex Kiernan void fastboot_nand_erase(const char *cmd, char *response)
237312a10f1SAlex Kiernan {
238312a10f1SAlex Kiernan struct part_info *part;
239312a10f1SAlex Kiernan struct mtd_info *mtd = NULL;
240312a10f1SAlex Kiernan int ret;
241312a10f1SAlex Kiernan
242c4ded03eSAlex Kiernan ret = fb_nand_lookup(cmd, &mtd, &part, response);
243312a10f1SAlex Kiernan if (ret) {
244312a10f1SAlex Kiernan pr_err("invalid NAND device");
245c4ded03eSAlex Kiernan fastboot_fail("invalid NAND device", response);
246312a10f1SAlex Kiernan return;
247312a10f1SAlex Kiernan }
248312a10f1SAlex Kiernan
249312a10f1SAlex Kiernan ret = board_fastboot_erase_partition_setup(part->name);
250312a10f1SAlex Kiernan if (ret)
251312a10f1SAlex Kiernan return;
252312a10f1SAlex Kiernan
253312a10f1SAlex Kiernan ret = _fb_nand_erase(mtd, part);
254312a10f1SAlex Kiernan if (ret) {
255312a10f1SAlex Kiernan pr_err("failed erasing from device %s", mtd->name);
256c4ded03eSAlex Kiernan fastboot_fail("failed erasing from device", response);
257312a10f1SAlex Kiernan return;
258312a10f1SAlex Kiernan }
259312a10f1SAlex Kiernan
260c4ded03eSAlex Kiernan fastboot_okay(NULL, response);
261312a10f1SAlex Kiernan }
262