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 struct fb_nand_sparse {
20 struct part_info *part;
23 __weak int board_fastboot_erase_partition_setup(char *name)
28 __weak int board_fastboot_write_partition_setup(char *name)
33 static int fb_nand_lookup(const char *partname,
34 struct mtd_info **mtd,
35 struct part_info **part)
37 struct mtd_device *dev;
41 ret = mtdparts_init();
43 pr_err("Cannot initialize MTD partitions\n");
44 fastboot_fail("cannot init mtdparts");
48 ret = find_dev_and_part(partname, &dev, &pnum, part);
50 pr_err("cannot find partition: '%s'", partname);
51 fastboot_fail("cannot find partition");
55 if (dev->id->type != MTD_DEV_TYPE_NAND) {
56 pr_err("partition '%s' is not stored on a NAND device",
58 fastboot_fail("not a NAND device");
62 *mtd = get_nand_dev_by_index(dev->id->num);
67 static int _fb_nand_erase(struct mtd_info *mtd, struct part_info *part)
69 nand_erase_options_t opts;
72 memset(&opts, 0, sizeof(opts));
73 opts.offset = part->offset;
74 opts.length = part->size;
77 printf("Erasing blocks 0x%llx to 0x%llx\n",
78 part->offset, part->offset + part->size);
80 ret = nand_erase_opts(mtd, &opts);
84 printf("........ erased 0x%llx bytes from '%s'\n",
85 part->size, part->name);
90 static int _fb_nand_write(struct mtd_info *mtd, struct part_info *part,
91 void *buffer, unsigned int offset,
92 unsigned int length, size_t *written)
94 int flags = WITH_WR_VERIFY;
96 #ifdef CONFIG_FASTBOOT_FLASH_NAND_TRIMFFS
97 flags |= WITH_DROP_FFS;
100 return nand_write_skip_bad(mtd, offset, &length, written,
101 part->size - (offset - part->offset),
105 static lbaint_t fb_nand_sparse_write(struct sparse_storage *info,
106 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
108 struct fb_nand_sparse *sparse = info->priv;
112 ret = _fb_nand_write(sparse->mtd, sparse->part, (void *)buffer,
114 blkcnt * info->blksz, &written);
116 printf("Failed to write sparse chunk\n");
120 /* TODO - verify that the value "written" includes the "bad-blocks" ... */
123 * the return value must be 'blkcnt' ("good-blocks") plus the
124 * number of "bad-blocks" encountered within this space...
126 return written / info->blksz;
129 static lbaint_t fb_nand_sparse_reserve(struct sparse_storage *info,
130 lbaint_t blk, lbaint_t blkcnt)
135 * TODO - implement a function to determine the total number
136 * of blocks which must be used in order to reserve the specified
137 * number ("blkcnt") of "good-blocks", starting at "blk"...
138 * ( possibly something like the "check_skip_len()" function )
142 * the return value must be 'blkcnt' ("good-blocks") plus the
143 * number of "bad-blocks" encountered within this space...
145 return blkcnt + bad_blocks;
148 void fb_nand_flash_write(const char *cmd, void *download_buffer,
149 unsigned int download_bytes)
151 struct part_info *part;
152 struct mtd_info *mtd = NULL;
155 ret = fb_nand_lookup(cmd, &mtd, &part);
157 pr_err("invalid NAND device");
158 fastboot_fail("invalid NAND device");
162 ret = board_fastboot_write_partition_setup(part->name);
166 if (is_sparse_image(download_buffer)) {
167 struct fb_nand_sparse sparse_priv;
168 struct sparse_storage sparse;
170 sparse_priv.mtd = mtd;
171 sparse_priv.part = part;
173 sparse.blksz = mtd->writesize;
174 sparse.start = part->offset / sparse.blksz;
175 sparse.size = part->size / sparse.blksz;
176 sparse.write = fb_nand_sparse_write;
177 sparse.reserve = fb_nand_sparse_reserve;
179 printf("Flashing sparse image at offset " LBAFU "\n",
182 sparse.priv = &sparse_priv;
183 write_sparse_image(&sparse, cmd, download_buffer,
186 printf("Flashing raw image at offset 0x%llx\n",
189 ret = _fb_nand_write(mtd, part, download_buffer, part->offset,
190 download_bytes, NULL);
192 printf("........ wrote %u bytes to '%s'\n",
193 download_bytes, part->name);
197 fastboot_fail("error writing the image");
204 void fb_nand_erase(const char *cmd)
206 struct part_info *part;
207 struct mtd_info *mtd = NULL;
210 ret = fb_nand_lookup(cmd, &mtd, &part);
212 pr_err("invalid NAND device");
213 fastboot_fail("invalid NAND device");
217 ret = board_fastboot_erase_partition_setup(part->name);
221 ret = _fb_nand_erase(mtd, part);
223 pr_err("failed erasing from device %s", mtd->name);
224 fastboot_fail("failed erasing from device");