1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2014 Broadcom Corporation.
4 * Copyright 2015 Free Electrons.
11 #include <image-sparse.h>
13 #include <linux/mtd/mtd.h>
14 #include <jffs2/jffs2.h>
17 struct fb_nand_sparse
{
19 struct part_info
*part
;
22 __weak
int board_fastboot_erase_partition_setup(char *name
)
27 __weak
int board_fastboot_write_partition_setup(char *name
)
32 static int fb_nand_lookup(const char *partname
,
33 struct mtd_info
**mtd
,
34 struct part_info
**part
)
36 struct mtd_device
*dev
;
40 ret
= mtdparts_init();
42 pr_err("Cannot initialize MTD partitions\n");
43 fastboot_fail("cannot init mtdparts");
47 ret
= find_dev_and_part(partname
, &dev
, &pnum
, part
);
49 pr_err("cannot find partition: '%s'", partname
);
50 fastboot_fail("cannot find partition");
54 if (dev
->id
->type
!= MTD_DEV_TYPE_NAND
) {
55 pr_err("partition '%s' is not stored on a NAND device",
57 fastboot_fail("not a NAND device");
61 *mtd
= get_nand_dev_by_index(dev
->id
->num
);
66 static int _fb_nand_erase(struct mtd_info
*mtd
, struct part_info
*part
)
68 nand_erase_options_t opts
;
71 memset(&opts
, 0, sizeof(opts
));
72 opts
.offset
= part
->offset
;
73 opts
.length
= part
->size
;
76 printf("Erasing blocks 0x%llx to 0x%llx\n",
77 part
->offset
, part
->offset
+ part
->size
);
79 ret
= nand_erase_opts(mtd
, &opts
);
83 printf("........ erased 0x%llx bytes from '%s'\n",
84 part
->size
, part
->name
);
89 static int _fb_nand_write(struct mtd_info
*mtd
, struct part_info
*part
,
90 void *buffer
, unsigned int offset
,
91 unsigned int length
, size_t *written
)
93 int flags
= WITH_WR_VERIFY
;
95 #ifdef CONFIG_FASTBOOT_FLASH_NAND_TRIMFFS
96 flags
|= WITH_DROP_FFS
;
99 return nand_write_skip_bad(mtd
, offset
, &length
, written
,
100 part
->size
- (offset
- part
->offset
),
104 static lbaint_t
fb_nand_sparse_write(struct sparse_storage
*info
,
105 lbaint_t blk
, lbaint_t blkcnt
, const void *buffer
)
107 struct fb_nand_sparse
*sparse
= info
->priv
;
111 ret
= _fb_nand_write(sparse
->mtd
, sparse
->part
, (void *)buffer
,
113 blkcnt
* info
->blksz
, &written
);
115 printf("Failed to write sparse chunk\n");
119 /* TODO - verify that the value "written" includes the "bad-blocks" ... */
122 * the return value must be 'blkcnt' ("good-blocks") plus the
123 * number of "bad-blocks" encountered within this space...
125 return written
/ info
->blksz
;
128 static lbaint_t
fb_nand_sparse_reserve(struct sparse_storage
*info
,
129 lbaint_t blk
, lbaint_t blkcnt
)
134 * TODO - implement a function to determine the total number
135 * of blocks which must be used in order to reserve the specified
136 * number ("blkcnt") of "good-blocks", starting at "blk"...
137 * ( possibly something like the "check_skip_len()" function )
141 * the return value must be 'blkcnt' ("good-blocks") plus the
142 * number of "bad-blocks" encountered within this space...
144 return blkcnt
+ bad_blocks
;
147 void fb_nand_flash_write(const char *cmd
, void *download_buffer
,
148 unsigned int download_bytes
)
150 struct part_info
*part
;
151 struct mtd_info
*mtd
= NULL
;
154 ret
= fb_nand_lookup(cmd
, &mtd
, &part
);
156 pr_err("invalid NAND device");
157 fastboot_fail("invalid NAND device");
161 ret
= board_fastboot_write_partition_setup(part
->name
);
165 if (is_sparse_image(download_buffer
)) {
166 struct fb_nand_sparse sparse_priv
;
167 struct sparse_storage sparse
;
169 sparse_priv
.mtd
= mtd
;
170 sparse_priv
.part
= part
;
172 sparse
.blksz
= mtd
->writesize
;
173 sparse
.start
= part
->offset
/ sparse
.blksz
;
174 sparse
.size
= part
->size
/ sparse
.blksz
;
175 sparse
.write
= fb_nand_sparse_write
;
176 sparse
.reserve
= fb_nand_sparse_reserve
;
177 sparse
.mssg
= fastboot_fail
;
179 printf("Flashing sparse image at offset " LBAFU
"\n",
182 sparse
.priv
= &sparse_priv
;
183 ret
= write_sparse_image(&sparse
, cmd
, download_buffer
);
187 printf("Flashing raw image at offset 0x%llx\n",
190 ret
= _fb_nand_write(mtd
, part
, download_buffer
, part
->offset
,
191 download_bytes
, NULL
);
193 printf("........ wrote %u bytes to '%s'\n",
194 download_bytes
, part
->name
);
198 fastboot_fail("error writing the image");
205 void fb_nand_erase(const char *cmd
)
207 struct part_info
*part
;
208 struct mtd_info
*mtd
= NULL
;
211 ret
= fb_nand_lookup(cmd
, &mtd
, &part
);
213 pr_err("invalid NAND device");
214 fastboot_fail("invalid NAND device");
218 ret
= board_fastboot_erase_partition_setup(part
->name
);
222 ret
= _fb_nand_erase(mtd
, part
);
224 pr_err("failed erasing from device %s", mtd
->name
);
225 fastboot_fail("failed erasing from device");