1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2014 Broadcom Corporation.
4 * Copyright 2015 Free Electrons.
13 #include <image-sparse.h>
15 #include <linux/mtd/mtd.h>
16 #include <jffs2/jffs2.h>
19 struct fb_nand_sparse {
21 struct part_info *part;
24 __weak int board_fastboot_erase_partition_setup(char *name)
29 __weak int board_fastboot_write_partition_setup(char *name)
34 static int fb_nand_lookup(const char *partname,
35 struct mtd_info **mtd,
36 struct part_info **part,
39 struct mtd_device *dev;
43 ret = mtdparts_init();
45 pr_err("Cannot initialize MTD partitions\n");
46 fastboot_fail("cannot init mtdparts", response);
50 ret = find_dev_and_part(partname, &dev, &pnum, part);
52 pr_err("cannot find partition: '%s'", partname);
53 fastboot_fail("cannot find partition", response);
57 if (dev->id->type != MTD_DEV_TYPE_NAND) {
58 pr_err("partition '%s' is not stored on a NAND device",
60 fastboot_fail("not a NAND device", response);
64 *mtd = get_nand_dev_by_index(dev->id->num);
69 static int _fb_nand_erase(struct mtd_info *mtd, struct part_info *part)
71 nand_erase_options_t opts;
74 memset(&opts, 0, sizeof(opts));
75 opts.offset = part->offset;
76 opts.length = part->size;
79 printf("Erasing blocks 0x%llx to 0x%llx\n",
80 part->offset, part->offset + part->size);
82 ret = nand_erase_opts(mtd, &opts);
86 printf("........ erased 0x%llx bytes from '%s'\n",
87 part->size, part->name);
92 static int _fb_nand_write(struct mtd_info *mtd, struct part_info *part,
93 void *buffer, u32 offset,
94 size_t length, size_t *written)
96 int flags = WITH_WR_VERIFY;
98 #ifdef CONFIG_FASTBOOT_FLASH_NAND_TRIMFFS
99 flags |= WITH_DROP_FFS;
102 return nand_write_skip_bad(mtd, offset, &length, written,
103 part->size - (offset - part->offset),
107 static lbaint_t fb_nand_sparse_write(struct sparse_storage *info,
108 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
110 struct fb_nand_sparse *sparse = info->priv;
114 ret = _fb_nand_write(sparse->mtd, sparse->part, (void *)buffer,
116 blkcnt * info->blksz, &written);
118 printf("Failed to write sparse chunk\n");
122 /* TODO - verify that the value "written" includes the "bad-blocks" ... */
125 * the return value must be 'blkcnt' ("good-blocks") plus the
126 * number of "bad-blocks" encountered within this space...
128 return written / info->blksz;
131 static lbaint_t fb_nand_sparse_reserve(struct sparse_storage *info,
132 lbaint_t blk, lbaint_t blkcnt)
137 * TODO - implement a function to determine the total number
138 * of blocks which must be used in order to reserve the specified
139 * number ("blkcnt") of "good-blocks", starting at "blk"...
140 * ( possibly something like the "check_skip_len()" function )
144 * the return value must be 'blkcnt' ("good-blocks") plus the
145 * number of "bad-blocks" encountered within this space...
147 return blkcnt + bad_blocks;
151 * fastboot_nand_get_part_info() - Lookup NAND partion by name
153 * @part_name: Named device to lookup
154 * @part_info: Pointer to returned part_info pointer
155 * @response: Pointer to fastboot response buffer
157 int fastboot_nand_get_part_info(const char *part_name,
158 struct part_info **part_info, char *response)
160 struct mtd_info *mtd = NULL;
162 return fb_nand_lookup(part_name, &mtd, part_info, response);
166 * fastboot_nand_flash_write() - Write image to NAND for fastboot
168 * @cmd: Named device to write image to
169 * @download_buffer: Pointer to image data
170 * @download_bytes: Size of image data
171 * @response: Pointer to fastboot response buffer
173 void fastboot_nand_flash_write(const char *cmd, void *download_buffer,
174 u32 download_bytes, char *response)
176 struct part_info *part;
177 struct mtd_info *mtd = NULL;
180 ret = fb_nand_lookup(cmd, &mtd, &part, response);
182 pr_err("invalid NAND device");
183 fastboot_fail("invalid NAND device", response);
187 ret = board_fastboot_write_partition_setup(part->name);
191 if (is_sparse_image(download_buffer)) {
192 struct fb_nand_sparse sparse_priv;
193 struct sparse_storage sparse;
195 sparse_priv.mtd = mtd;
196 sparse_priv.part = part;
198 sparse.blksz = mtd->writesize;
199 sparse.start = part->offset / sparse.blksz;
200 sparse.size = part->size / sparse.blksz;
201 sparse.write = fb_nand_sparse_write;
202 sparse.reserve = fb_nand_sparse_reserve;
203 sparse.mssg = fastboot_fail;
205 printf("Flashing sparse image at offset " LBAFU "\n",
208 sparse.priv = &sparse_priv;
209 ret = write_sparse_image(&sparse, cmd, download_buffer,
212 fastboot_okay(NULL, response);
214 printf("Flashing raw image at offset 0x%llx\n",
217 ret = _fb_nand_write(mtd, part, download_buffer, part->offset,
218 download_bytes, NULL);
220 printf("........ wrote %u bytes to '%s'\n",
221 download_bytes, part->name);
225 fastboot_fail("error writing the image", response);
229 fastboot_okay(NULL, response);
233 * fastboot_nand_flash_erase() - Erase NAND for fastboot
235 * @cmd: Named device to erase
236 * @response: Pointer to fastboot response buffer
238 void fastboot_nand_erase(const char *cmd, char *response)
240 struct part_info *part;
241 struct mtd_info *mtd = NULL;
244 ret = fb_nand_lookup(cmd, &mtd, &part, response);
246 pr_err("invalid NAND device");
247 fastboot_fail("invalid NAND device", response);
251 ret = board_fastboot_erase_partition_setup(part->name);
255 ret = _fb_nand_erase(mtd, part);
257 pr_err("failed erasing from device %s", mtd->name);
258 fastboot_fail("failed erasing from device", response);
262 fastboot_okay(NULL, response);