blob: bbe26ddcc9bedf9f48399914f2951808da41c14d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Maxime Ripard33602a92015-10-15 14:34:17 +02002/*
3 * Copyright 2014 Broadcom Corporation.
4 * Copyright 2015 Free Electrons.
Maxime Ripard33602a92015-10-15 14:34:17 +02005 */
6
7#include <config.h>
8#include <common.h>
Simon Glass655306c2020-05-10 11:39:58 -06009#include <blk.h>
Maxime Ripard33602a92015-10-15 14:34:17 +020010
Maxime Ripard33602a92015-10-15 14:34:17 +020011#include <fastboot.h>
Maxime Ripard6af49bc2015-10-15 14:34:19 +020012#include <image-sparse.h>
Maxime Ripard33602a92015-10-15 14:34:17 +020013
Simon Glassbdd5f812023-09-14 18:21:46 -060014#include <linux/printk.h>
Maxime Ripard33602a92015-10-15 14:34:17 +020015#include <linux/mtd/mtd.h>
16#include <jffs2/jffs2.h>
17#include <nand.h>
18
Maxime Ripard33602a92015-10-15 14:34:17 +020019struct fb_nand_sparse {
Steve Rae8cf58db2016-06-07 11:19:36 -070020 struct mtd_info *mtd;
Maxime Ripard33602a92015-10-15 14:34:17 +020021 struct part_info *part;
22};
23
Maxime Ripard18123542015-10-15 14:34:18 +020024__weak int board_fastboot_erase_partition_setup(char *name)
25{
26 return 0;
27}
28
29__weak int board_fastboot_write_partition_setup(char *name)
30{
31 return 0;
32}
33
Steve Rae76336b22016-06-07 11:19:37 -070034static int fb_nand_lookup(const char *partname,
Sergey Kubushyn5ad7e832016-06-07 14:22:59 -070035 struct mtd_info **mtd,
Alex Kiernan27b69de2018-05-29 15:30:40 +000036 struct part_info **part,
37 char *response)
Maxime Ripard33602a92015-10-15 14:34:17 +020038{
39 struct mtd_device *dev;
40 int ret;
41 u8 pnum;
42
43 ret = mtdparts_init();
44 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090045 pr_err("Cannot initialize MTD partitions\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +000046 fastboot_fail("cannot init mtdparts", response);
Maxime Ripard33602a92015-10-15 14:34:17 +020047 return ret;
48 }
49
50 ret = find_dev_and_part(partname, &dev, &pnum, part);
51 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090052 pr_err("cannot find partition: '%s'", partname);
Alex Kiernan27b69de2018-05-29 15:30:40 +000053 fastboot_fail("cannot find partition", response);
Maxime Ripard33602a92015-10-15 14:34:17 +020054 return ret;
55 }
56
57 if (dev->id->type != MTD_DEV_TYPE_NAND) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090058 pr_err("partition '%s' is not stored on a NAND device",
Maxime Ripard33602a92015-10-15 14:34:17 +020059 partname);
Alex Kiernan27b69de2018-05-29 15:30:40 +000060 fastboot_fail("not a NAND device", response);
Maxime Ripard33602a92015-10-15 14:34:17 +020061 return -EINVAL;
62 }
63
Grygorii Strashko5f33b822017-06-26 19:12:56 -050064 *mtd = get_nand_dev_by_index(dev->id->num);
Maxime Ripard33602a92015-10-15 14:34:17 +020065
66 return 0;
67}
68
Scott Wood08364d92016-05-30 13:57:54 -050069static int _fb_nand_erase(struct mtd_info *mtd, struct part_info *part)
Maxime Ripard33602a92015-10-15 14:34:17 +020070{
71 nand_erase_options_t opts;
72 int ret;
73
74 memset(&opts, 0, sizeof(opts));
75 opts.offset = part->offset;
76 opts.length = part->size;
77 opts.quiet = 1;
78
79 printf("Erasing blocks 0x%llx to 0x%llx\n",
80 part->offset, part->offset + part->size);
81
Scott Wood08364d92016-05-30 13:57:54 -050082 ret = nand_erase_opts(mtd, &opts);
Maxime Ripard33602a92015-10-15 14:34:17 +020083 if (ret)
84 return ret;
85
86 printf("........ erased 0x%llx bytes from '%s'\n",
87 part->size, part->name);
88
89 return 0;
90}
91
Scott Wood08364d92016-05-30 13:57:54 -050092static int _fb_nand_write(struct mtd_info *mtd, struct part_info *part,
Alex Kiernand5aa57c2018-05-29 15:30:53 +000093 void *buffer, u32 offset,
Alex Kiernand0f91f02018-05-29 15:30:45 +000094 size_t length, size_t *written)
Maxime Ripard33602a92015-10-15 14:34:17 +020095{
96 int flags = WITH_WR_VERIFY;
97
98#ifdef CONFIG_FASTBOOT_FLASH_NAND_TRIMFFS
99 flags |= WITH_DROP_FFS;
100#endif
101
Scott Wood08364d92016-05-30 13:57:54 -0500102 return nand_write_skip_bad(mtd, offset, &length, written,
Maxime Ripard33602a92015-10-15 14:34:17 +0200103 part->size - (offset - part->offset),
104 buffer, flags);
105}
106
Steve Rae8cf58db2016-06-07 11:19:36 -0700107static lbaint_t fb_nand_sparse_write(struct sparse_storage *info,
108 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
Maxime Ripard33602a92015-10-15 14:34:17 +0200109{
Steve Rae8cf58db2016-06-07 11:19:36 -0700110 struct fb_nand_sparse *sparse = info->priv;
Maxime Ripard33602a92015-10-15 14:34:17 +0200111 size_t written;
112 int ret;
113
Steve Rae8cf58db2016-06-07 11:19:36 -0700114 ret = _fb_nand_write(sparse->mtd, sparse->part, (void *)buffer,
115 blk * info->blksz,
116 blkcnt * info->blksz, &written);
Maxime Ripard33602a92015-10-15 14:34:17 +0200117 if (ret < 0) {
118 printf("Failed to write sparse chunk\n");
119 return ret;
120 }
121
Steve Rae8cf58db2016-06-07 11:19:36 -0700122/* TODO - verify that the value "written" includes the "bad-blocks" ... */
123
124 /*
125 * the return value must be 'blkcnt' ("good-blocks") plus the
126 * number of "bad-blocks" encountered within this space...
127 */
128 return written / info->blksz;
Maxime Ripard33602a92015-10-15 14:34:17 +0200129}
130
Steve Rae8a99be82016-06-07 11:19:38 -0700131static lbaint_t fb_nand_sparse_reserve(struct sparse_storage *info,
132 lbaint_t blk, lbaint_t blkcnt)
133{
134 int bad_blocks = 0;
135
136/*
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 )
141 */
142
143 /*
144 * the return value must be 'blkcnt' ("good-blocks") plus the
145 * number of "bad-blocks" encountered within this space...
146 */
147 return blkcnt + bad_blocks;
148}
149
Alex Kiernan32c21942018-05-29 15:30:48 +0000150/**
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000151 * fastboot_nand_get_part_info() - Lookup NAND partion by name
152 *
153 * @part_name: Named device to lookup
154 * @part_info: Pointer to returned part_info pointer
155 * @response: Pointer to fastboot response buffer
156 */
Sam Protsenko0fb37ee2019-06-13 21:11:07 +0300157int fastboot_nand_get_part_info(const char *part_name,
158 struct part_info **part_info, char *response)
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000159{
160 struct mtd_info *mtd = NULL;
161
162 return fb_nand_lookup(part_name, &mtd, part_info, response);
163}
164
165/**
Alex Kiernan32c21942018-05-29 15:30:48 +0000166 * fastboot_nand_flash_write() - Write image to NAND for fastboot
167 *
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
172 */
173void fastboot_nand_flash_write(const char *cmd, void *download_buffer,
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000174 u32 download_bytes, char *response)
Maxime Ripard33602a92015-10-15 14:34:17 +0200175{
176 struct part_info *part;
Scott Wood08364d92016-05-30 13:57:54 -0500177 struct mtd_info *mtd = NULL;
Maxime Ripard33602a92015-10-15 14:34:17 +0200178 int ret;
179
Alex Kiernan27b69de2018-05-29 15:30:40 +0000180 ret = fb_nand_lookup(cmd, &mtd, &part, response);
Maxime Ripard33602a92015-10-15 14:34:17 +0200181 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900182 pr_err("invalid NAND device");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000183 fastboot_fail("invalid NAND device", response);
Maxime Ripard33602a92015-10-15 14:34:17 +0200184 return;
185 }
186
Maxime Ripard18123542015-10-15 14:34:18 +0200187 ret = board_fastboot_write_partition_setup(part->name);
188 if (ret)
189 return;
190
Maxime Ripard33602a92015-10-15 14:34:17 +0200191 if (is_sparse_image(download_buffer)) {
192 struct fb_nand_sparse sparse_priv;
Steve Rae8cf58db2016-06-07 11:19:36 -0700193 struct sparse_storage sparse;
Maxime Ripard33602a92015-10-15 14:34:17 +0200194
Steve Rae8cf58db2016-06-07 11:19:36 -0700195 sparse_priv.mtd = mtd;
Maxime Ripard33602a92015-10-15 14:34:17 +0200196 sparse_priv.part = part;
197
Steve Rae8cf58db2016-06-07 11:19:36 -0700198 sparse.blksz = mtd->writesize;
199 sparse.start = part->offset / sparse.blksz;
200 sparse.size = part->size / sparse.blksz;
Maxime Ripard33602a92015-10-15 14:34:17 +0200201 sparse.write = fb_nand_sparse_write;
Steve Rae8a99be82016-06-07 11:19:38 -0700202 sparse.reserve = fb_nand_sparse_reserve;
Jassi Brar427e6692018-04-06 12:05:09 +0530203 sparse.mssg = fastboot_fail;
Maxime Ripard33602a92015-10-15 14:34:17 +0200204
Steve Rae8cf58db2016-06-07 11:19:36 -0700205 printf("Flashing sparse image at offset " LBAFU "\n",
206 sparse.start);
207
208 sparse.priv = &sparse_priv;
Alex Kiernan27b69de2018-05-29 15:30:40 +0000209 ret = write_sparse_image(&sparse, cmd, download_buffer,
210 response);
Jassi Brar427e6692018-04-06 12:05:09 +0530211 if (!ret)
Alex Kiernan27b69de2018-05-29 15:30:40 +0000212 fastboot_okay(NULL, response);
Maxime Ripard33602a92015-10-15 14:34:17 +0200213 } else {
214 printf("Flashing raw image at offset 0x%llx\n",
215 part->offset);
216
Scott Wood08364d92016-05-30 13:57:54 -0500217 ret = _fb_nand_write(mtd, part, download_buffer, part->offset,
Maxime Ripard33602a92015-10-15 14:34:17 +0200218 download_bytes, NULL);
219
220 printf("........ wrote %u bytes to '%s'\n",
221 download_bytes, part->name);
222 }
223
224 if (ret) {
Alex Kiernan27b69de2018-05-29 15:30:40 +0000225 fastboot_fail("error writing the image", response);
Maxime Ripard33602a92015-10-15 14:34:17 +0200226 return;
227 }
228
Alex Kiernan27b69de2018-05-29 15:30:40 +0000229 fastboot_okay(NULL, response);
Maxime Ripard33602a92015-10-15 14:34:17 +0200230}
231
Alex Kiernan32c21942018-05-29 15:30:48 +0000232/**
233 * fastboot_nand_flash_erase() - Erase NAND for fastboot
234 *
235 * @cmd: Named device to erase
236 * @response: Pointer to fastboot response buffer
237 */
238void fastboot_nand_erase(const char *cmd, char *response)
Maxime Ripard33602a92015-10-15 14:34:17 +0200239{
240 struct part_info *part;
Scott Wood08364d92016-05-30 13:57:54 -0500241 struct mtd_info *mtd = NULL;
Maxime Ripard33602a92015-10-15 14:34:17 +0200242 int ret;
243
Alex Kiernan27b69de2018-05-29 15:30:40 +0000244 ret = fb_nand_lookup(cmd, &mtd, &part, response);
Maxime Ripard33602a92015-10-15 14:34:17 +0200245 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900246 pr_err("invalid NAND device");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000247 fastboot_fail("invalid NAND device", response);
Maxime Ripard33602a92015-10-15 14:34:17 +0200248 return;
249 }
250
Maxime Ripard18123542015-10-15 14:34:18 +0200251 ret = board_fastboot_erase_partition_setup(part->name);
252 if (ret)
253 return;
254
Scott Wood08364d92016-05-30 13:57:54 -0500255 ret = _fb_nand_erase(mtd, part);
Maxime Ripard33602a92015-10-15 14:34:17 +0200256 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900257 pr_err("failed erasing from device %s", mtd->name);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000258 fastboot_fail("failed erasing from device", response);
Maxime Ripard33602a92015-10-15 14:34:17 +0200259 return;
260 }
261
Alex Kiernan27b69de2018-05-29 15:30:40 +0000262 fastboot_okay(NULL, response);
Maxime Ripard33602a92015-10-15 14:34:17 +0200263}