blob: 526bc12307f8a071b9a66ad8b28b008dcda2d29e [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>
9
Maxime Ripard33602a92015-10-15 14:34:17 +020010#include <fastboot.h>
Maxime Ripard6af49bc2015-10-15 14:34:19 +020011#include <image-sparse.h>
Maxime Ripard33602a92015-10-15 14:34:17 +020012
13#include <linux/mtd/mtd.h>
14#include <jffs2/jffs2.h>
15#include <nand.h>
16
Maxime Ripard33602a92015-10-15 14:34:17 +020017struct fb_nand_sparse {
Steve Rae8cf58db2016-06-07 11:19:36 -070018 struct mtd_info *mtd;
Maxime Ripard33602a92015-10-15 14:34:17 +020019 struct part_info *part;
20};
21
Maxime Ripard18123542015-10-15 14:34:18 +020022__weak int board_fastboot_erase_partition_setup(char *name)
23{
24 return 0;
25}
26
27__weak int board_fastboot_write_partition_setup(char *name)
28{
29 return 0;
30}
31
Steve Rae76336b22016-06-07 11:19:37 -070032static int fb_nand_lookup(const char *partname,
Sergey Kubushyn5ad7e832016-06-07 14:22:59 -070033 struct mtd_info **mtd,
Alex Kiernan27b69de2018-05-29 15:30:40 +000034 struct part_info **part,
35 char *response)
Maxime Ripard33602a92015-10-15 14:34:17 +020036{
37 struct mtd_device *dev;
38 int ret;
39 u8 pnum;
40
41 ret = mtdparts_init();
42 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090043 pr_err("Cannot initialize MTD partitions\n");
Alex Kiernan27b69de2018-05-29 15:30:40 +000044 fastboot_fail("cannot init mtdparts", response);
Maxime Ripard33602a92015-10-15 14:34:17 +020045 return ret;
46 }
47
48 ret = find_dev_and_part(partname, &dev, &pnum, part);
49 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090050 pr_err("cannot find partition: '%s'", partname);
Alex Kiernan27b69de2018-05-29 15:30:40 +000051 fastboot_fail("cannot find partition", response);
Maxime Ripard33602a92015-10-15 14:34:17 +020052 return ret;
53 }
54
55 if (dev->id->type != MTD_DEV_TYPE_NAND) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090056 pr_err("partition '%s' is not stored on a NAND device",
Maxime Ripard33602a92015-10-15 14:34:17 +020057 partname);
Alex Kiernan27b69de2018-05-29 15:30:40 +000058 fastboot_fail("not a NAND device", response);
Maxime Ripard33602a92015-10-15 14:34:17 +020059 return -EINVAL;
60 }
61
Grygorii Strashko5f33b822017-06-26 19:12:56 -050062 *mtd = get_nand_dev_by_index(dev->id->num);
Maxime Ripard33602a92015-10-15 14:34:17 +020063
64 return 0;
65}
66
Scott Wood08364d92016-05-30 13:57:54 -050067static int _fb_nand_erase(struct mtd_info *mtd, struct part_info *part)
Maxime Ripard33602a92015-10-15 14:34:17 +020068{
69 nand_erase_options_t opts;
70 int ret;
71
72 memset(&opts, 0, sizeof(opts));
73 opts.offset = part->offset;
74 opts.length = part->size;
75 opts.quiet = 1;
76
77 printf("Erasing blocks 0x%llx to 0x%llx\n",
78 part->offset, part->offset + part->size);
79
Scott Wood08364d92016-05-30 13:57:54 -050080 ret = nand_erase_opts(mtd, &opts);
Maxime Ripard33602a92015-10-15 14:34:17 +020081 if (ret)
82 return ret;
83
84 printf("........ erased 0x%llx bytes from '%s'\n",
85 part->size, part->name);
86
87 return 0;
88}
89
Scott Wood08364d92016-05-30 13:57:54 -050090static int _fb_nand_write(struct mtd_info *mtd, struct part_info *part,
Alex Kiernand5aa57c2018-05-29 15:30:53 +000091 void *buffer, u32 offset,
Alex Kiernand0f91f02018-05-29 15:30:45 +000092 size_t length, size_t *written)
Maxime Ripard33602a92015-10-15 14:34:17 +020093{
94 int flags = WITH_WR_VERIFY;
95
96#ifdef CONFIG_FASTBOOT_FLASH_NAND_TRIMFFS
97 flags |= WITH_DROP_FFS;
98#endif
99
Scott Wood08364d92016-05-30 13:57:54 -0500100 return nand_write_skip_bad(mtd, offset, &length, written,
Maxime Ripard33602a92015-10-15 14:34:17 +0200101 part->size - (offset - part->offset),
102 buffer, flags);
103}
104
Steve Rae8cf58db2016-06-07 11:19:36 -0700105static lbaint_t fb_nand_sparse_write(struct sparse_storage *info,
106 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
Maxime Ripard33602a92015-10-15 14:34:17 +0200107{
Steve Rae8cf58db2016-06-07 11:19:36 -0700108 struct fb_nand_sparse *sparse = info->priv;
Maxime Ripard33602a92015-10-15 14:34:17 +0200109 size_t written;
110 int ret;
111
Steve Rae8cf58db2016-06-07 11:19:36 -0700112 ret = _fb_nand_write(sparse->mtd, sparse->part, (void *)buffer,
113 blk * info->blksz,
114 blkcnt * info->blksz, &written);
Maxime Ripard33602a92015-10-15 14:34:17 +0200115 if (ret < 0) {
116 printf("Failed to write sparse chunk\n");
117 return ret;
118 }
119
Steve Rae8cf58db2016-06-07 11:19:36 -0700120/* TODO - verify that the value "written" includes the "bad-blocks" ... */
121
122 /*
123 * the return value must be 'blkcnt' ("good-blocks") plus the
124 * number of "bad-blocks" encountered within this space...
125 */
126 return written / info->blksz;
Maxime Ripard33602a92015-10-15 14:34:17 +0200127}
128
Steve Rae8a99be82016-06-07 11:19:38 -0700129static lbaint_t fb_nand_sparse_reserve(struct sparse_storage *info,
130 lbaint_t blk, lbaint_t blkcnt)
131{
132 int bad_blocks = 0;
133
134/*
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 )
139 */
140
141 /*
142 * the return value must be 'blkcnt' ("good-blocks") plus the
143 * number of "bad-blocks" encountered within this space...
144 */
145 return blkcnt + bad_blocks;
146}
147
Alex Kiernan32c21942018-05-29 15:30:48 +0000148/**
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000149 * fastboot_nand_get_part_info() - Lookup NAND partion by name
150 *
151 * @part_name: Named device to lookup
152 * @part_info: Pointer to returned part_info pointer
153 * @response: Pointer to fastboot response buffer
154 */
155int fastboot_nand_get_part_info(char *part_name, struct part_info **part_info,
156 char *response)
157{
158 struct mtd_info *mtd = NULL;
159
160 return fb_nand_lookup(part_name, &mtd, part_info, response);
161}
162
163/**
Alex Kiernan32c21942018-05-29 15:30:48 +0000164 * fastboot_nand_flash_write() - Write image to NAND for fastboot
165 *
166 * @cmd: Named device to write image to
167 * @download_buffer: Pointer to image data
168 * @download_bytes: Size of image data
169 * @response: Pointer to fastboot response buffer
170 */
171void fastboot_nand_flash_write(const char *cmd, void *download_buffer,
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000172 u32 download_bytes, char *response)
Maxime Ripard33602a92015-10-15 14:34:17 +0200173{
174 struct part_info *part;
Scott Wood08364d92016-05-30 13:57:54 -0500175 struct mtd_info *mtd = NULL;
Maxime Ripard33602a92015-10-15 14:34:17 +0200176 int ret;
177
Alex Kiernan27b69de2018-05-29 15:30:40 +0000178 ret = fb_nand_lookup(cmd, &mtd, &part, response);
Maxime Ripard33602a92015-10-15 14:34:17 +0200179 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900180 pr_err("invalid NAND device");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000181 fastboot_fail("invalid NAND device", response);
Maxime Ripard33602a92015-10-15 14:34:17 +0200182 return;
183 }
184
Maxime Ripard18123542015-10-15 14:34:18 +0200185 ret = board_fastboot_write_partition_setup(part->name);
186 if (ret)
187 return;
188
Maxime Ripard33602a92015-10-15 14:34:17 +0200189 if (is_sparse_image(download_buffer)) {
190 struct fb_nand_sparse sparse_priv;
Steve Rae8cf58db2016-06-07 11:19:36 -0700191 struct sparse_storage sparse;
Maxime Ripard33602a92015-10-15 14:34:17 +0200192
Steve Rae8cf58db2016-06-07 11:19:36 -0700193 sparse_priv.mtd = mtd;
Maxime Ripard33602a92015-10-15 14:34:17 +0200194 sparse_priv.part = part;
195
Steve Rae8cf58db2016-06-07 11:19:36 -0700196 sparse.blksz = mtd->writesize;
197 sparse.start = part->offset / sparse.blksz;
198 sparse.size = part->size / sparse.blksz;
Maxime Ripard33602a92015-10-15 14:34:17 +0200199 sparse.write = fb_nand_sparse_write;
Steve Rae8a99be82016-06-07 11:19:38 -0700200 sparse.reserve = fb_nand_sparse_reserve;
Jassi Brar427e6692018-04-06 12:05:09 +0530201 sparse.mssg = fastboot_fail;
Maxime Ripard33602a92015-10-15 14:34:17 +0200202
Steve Rae8cf58db2016-06-07 11:19:36 -0700203 printf("Flashing sparse image at offset " LBAFU "\n",
204 sparse.start);
205
206 sparse.priv = &sparse_priv;
Alex Kiernan27b69de2018-05-29 15:30:40 +0000207 ret = write_sparse_image(&sparse, cmd, download_buffer,
208 response);
Jassi Brar427e6692018-04-06 12:05:09 +0530209 if (!ret)
Alex Kiernan27b69de2018-05-29 15:30:40 +0000210 fastboot_okay(NULL, response);
Maxime Ripard33602a92015-10-15 14:34:17 +0200211 } else {
212 printf("Flashing raw image at offset 0x%llx\n",
213 part->offset);
214
Scott Wood08364d92016-05-30 13:57:54 -0500215 ret = _fb_nand_write(mtd, part, download_buffer, part->offset,
Maxime Ripard33602a92015-10-15 14:34:17 +0200216 download_bytes, NULL);
217
218 printf("........ wrote %u bytes to '%s'\n",
219 download_bytes, part->name);
220 }
221
222 if (ret) {
Alex Kiernan27b69de2018-05-29 15:30:40 +0000223 fastboot_fail("error writing the image", response);
Maxime Ripard33602a92015-10-15 14:34:17 +0200224 return;
225 }
226
Alex Kiernan27b69de2018-05-29 15:30:40 +0000227 fastboot_okay(NULL, response);
Maxime Ripard33602a92015-10-15 14:34:17 +0200228}
229
Alex Kiernan32c21942018-05-29 15:30:48 +0000230/**
231 * fastboot_nand_flash_erase() - Erase NAND for fastboot
232 *
233 * @cmd: Named device to erase
234 * @response: Pointer to fastboot response buffer
235 */
236void fastboot_nand_erase(const char *cmd, char *response)
Maxime Ripard33602a92015-10-15 14:34:17 +0200237{
238 struct part_info *part;
Scott Wood08364d92016-05-30 13:57:54 -0500239 struct mtd_info *mtd = NULL;
Maxime Ripard33602a92015-10-15 14:34:17 +0200240 int ret;
241
Alex Kiernan27b69de2018-05-29 15:30:40 +0000242 ret = fb_nand_lookup(cmd, &mtd, &part, response);
Maxime Ripard33602a92015-10-15 14:34:17 +0200243 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900244 pr_err("invalid NAND device");
Alex Kiernan27b69de2018-05-29 15:30:40 +0000245 fastboot_fail("invalid NAND device", response);
Maxime Ripard33602a92015-10-15 14:34:17 +0200246 return;
247 }
248
Maxime Ripard18123542015-10-15 14:34:18 +0200249 ret = board_fastboot_erase_partition_setup(part->name);
250 if (ret)
251 return;
252
Scott Wood08364d92016-05-30 13:57:54 -0500253 ret = _fb_nand_erase(mtd, part);
Maxime Ripard33602a92015-10-15 14:34:17 +0200254 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900255 pr_err("failed erasing from device %s", mtd->name);
Alex Kiernan27b69de2018-05-29 15:30:40 +0000256 fastboot_fail("failed erasing from device", response);
Maxime Ripard33602a92015-10-15 14:34:17 +0200257 return;
258 }
259
Alex Kiernan27b69de2018-05-29 15:30:40 +0000260 fastboot_okay(NULL, response);
Maxime Ripard33602a92015-10-15 14:34:17 +0200261}