2 * Copyright 2014 Broadcom Corporation.
3 * Copyright 2015 Free Electrons.
5 * SPDX-License-Identifier: GPL-2.0+
12 #include <image-sparse.h>
14 #include <linux/mtd/mtd.h>
15 #include <jffs2/jffs2.h>
18 static char *response_str;
20 struct fb_nand_sparse {
22 struct part_info *part;
25 __weak int board_fastboot_erase_partition_setup(char *name)
30 __weak int board_fastboot_write_partition_setup(char *name)
35 static int fb_nand_lookup(const char *partname, char *response,
36 struct mtd_info **mtd,
37 struct part_info **part)
39 struct mtd_device *dev;
43 ret = mtdparts_init();
45 error("Cannot initialize MTD partitions\n");
46 fastboot_fail(response_str, "cannot init mtdparts");
50 ret = find_dev_and_part(partname, &dev, &pnum, part);
52 error("cannot find partition: '%s'", partname);
53 fastboot_fail(response_str, "cannot find partition");
57 if (dev->id->type != MTD_DEV_TYPE_NAND) {
58 error("partition '%s' is not stored on a NAND device",
60 fastboot_fail(response_str, "not a NAND device");
64 *mtd = nand_info[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, unsigned int offset,
94 unsigned int 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 void fb_nand_flash_write(const char *cmd, void *download_buffer,
132 unsigned int download_bytes, char *response)
134 struct part_info *part;
135 struct mtd_info *mtd = NULL;
138 /* initialize the response buffer */
139 response_str = response;
141 ret = fb_nand_lookup(cmd, response, &mtd, &part);
143 error("invalid NAND device");
144 fastboot_fail(response_str, "invalid NAND device");
148 ret = board_fastboot_write_partition_setup(part->name);
152 if (is_sparse_image(download_buffer)) {
153 struct fb_nand_sparse sparse_priv;
154 struct sparse_storage sparse;
156 sparse_priv.mtd = mtd;
157 sparse_priv.part = part;
159 sparse.blksz = mtd->writesize;
160 sparse.start = part->offset / sparse.blksz;
161 sparse.size = part->size / sparse.blksz;
162 sparse.write = fb_nand_sparse_write;
164 printf("Flashing sparse image at offset " LBAFU "\n",
167 sparse.priv = &sparse_priv;
168 write_sparse_image(&sparse, cmd, download_buffer,
169 download_bytes, response_str);
171 printf("Flashing raw image at offset 0x%llx\n",
174 ret = _fb_nand_write(mtd, part, download_buffer, part->offset,
175 download_bytes, NULL);
177 printf("........ wrote %u bytes to '%s'\n",
178 download_bytes, part->name);
182 fastboot_fail(response_str, "error writing the image");
186 fastboot_okay(response_str, "");
189 void fb_nand_erase(const char *cmd, char *response)
191 struct part_info *part;
192 struct mtd_info *mtd = NULL;
195 /* initialize the response buffer */
196 response_str = response;
198 ret = fb_nand_lookup(cmd, response, &mtd, &part);
200 error("invalid NAND device");
201 fastboot_fail(response_str, "invalid NAND device");
205 ret = board_fastboot_erase_partition_setup(part->name);
209 ret = _fb_nand_erase(mtd, part);
211 error("failed erasing from device %s", mtd->name);
212 fastboot_fail(response_str, "failed erasing from device");
216 fastboot_okay(response_str, "");