test: rng: Add a UT testcase for the rng command
[platform/kernel/u-boot.git] / drivers / fastboot / fb_nand.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2014 Broadcom Corporation.
4  * Copyright 2015 Free Electrons.
5  */
6
7 #include <config.h>
8 #include <common.h>
9 #include <blk.h>
10 #include <flash.h>
11
12 #include <fastboot.h>
13 #include <image-sparse.h>
14
15 #include <linux/mtd/mtd.h>
16 #include <jffs2/jffs2.h>
17 #include <nand.h>
18
19 struct fb_nand_sparse {
20         struct mtd_info         *mtd;
21         struct part_info        *part;
22 };
23
24 __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
34 static int fb_nand_lookup(const char *partname,
35                           struct mtd_info **mtd,
36                           struct part_info **part,
37                           char *response)
38 {
39         struct mtd_device *dev;
40         int ret;
41         u8 pnum;
42
43         ret = mtdparts_init();
44         if (ret) {
45                 pr_err("Cannot initialize MTD partitions\n");
46                 fastboot_fail("cannot init mtdparts", response);
47                 return ret;
48         }
49
50         ret = find_dev_and_part(partname, &dev, &pnum, part);
51         if (ret) {
52                 pr_err("cannot find partition: '%s'", partname);
53                 fastboot_fail("cannot find partition", response);
54                 return ret;
55         }
56
57         if (dev->id->type != MTD_DEV_TYPE_NAND) {
58                 pr_err("partition '%s' is not stored on a NAND device",
59                       partname);
60                 fastboot_fail("not a NAND device", response);
61                 return -EINVAL;
62         }
63
64         *mtd = get_nand_dev_by_index(dev->id->num);
65
66         return 0;
67 }
68
69 static int _fb_nand_erase(struct mtd_info *mtd, struct part_info *part)
70 {
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
82         ret = nand_erase_opts(mtd, &opts);
83         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
92 static int _fb_nand_write(struct mtd_info *mtd, struct part_info *part,
93                           void *buffer, u32 offset,
94                           size_t length, size_t *written)
95 {
96         int flags = WITH_WR_VERIFY;
97
98 #ifdef CONFIG_FASTBOOT_FLASH_NAND_TRIMFFS
99         flags |= WITH_DROP_FFS;
100 #endif
101
102         return nand_write_skip_bad(mtd, offset, &length, written,
103                                    part->size - (offset - part->offset),
104                                    buffer, flags);
105 }
106
107 static lbaint_t fb_nand_sparse_write(struct sparse_storage *info,
108                 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
109 {
110         struct fb_nand_sparse *sparse = info->priv;
111         size_t written;
112         int ret;
113
114         ret = _fb_nand_write(sparse->mtd, sparse->part, (void *)buffer,
115                              blk * info->blksz,
116                              blkcnt * info->blksz, &written);
117         if (ret < 0) {
118                 printf("Failed to write sparse chunk\n");
119                 return ret;
120         }
121
122 /* 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;
129 }
130
131 static 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
150 /**
151  * 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  */
157 int fastboot_nand_get_part_info(const char *part_name,
158                                 struct part_info **part_info, char *response)
159 {
160         struct mtd_info *mtd = NULL;
161
162         return fb_nand_lookup(part_name, &mtd, part_info, response);
163 }
164
165 /**
166  * 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  */
173 void fastboot_nand_flash_write(const char *cmd, void *download_buffer,
174                                u32 download_bytes, char *response)
175 {
176         struct part_info *part;
177         struct mtd_info *mtd = NULL;
178         int ret;
179
180         ret = fb_nand_lookup(cmd, &mtd, &part, response);
181         if (ret) {
182                 pr_err("invalid NAND device");
183                 fastboot_fail("invalid NAND device", response);
184                 return;
185         }
186
187         ret = board_fastboot_write_partition_setup(part->name);
188         if (ret)
189                 return;
190
191         if (is_sparse_image(download_buffer)) {
192                 struct fb_nand_sparse sparse_priv;
193                 struct sparse_storage sparse;
194
195                 sparse_priv.mtd = mtd;
196                 sparse_priv.part = part;
197
198                 sparse.blksz = mtd->writesize;
199                 sparse.start = part->offset / sparse.blksz;
200                 sparse.size = part->size / sparse.blksz;
201                 sparse.write = fb_nand_sparse_write;
202                 sparse.reserve = fb_nand_sparse_reserve;
203                 sparse.mssg = fastboot_fail;
204
205                 printf("Flashing sparse image at offset " LBAFU "\n",
206                        sparse.start);
207
208                 sparse.priv = &sparse_priv;
209                 ret = write_sparse_image(&sparse, cmd, download_buffer,
210                                          response);
211                 if (!ret)
212                         fastboot_okay(NULL, response);
213         } else {
214                 printf("Flashing raw image at offset 0x%llx\n",
215                        part->offset);
216
217                 ret = _fb_nand_write(mtd, part, download_buffer, part->offset,
218                                      download_bytes, NULL);
219
220                 printf("........ wrote %u bytes to '%s'\n",
221                        download_bytes, part->name);
222         }
223
224         if (ret) {
225                 fastboot_fail("error writing the image", response);
226                 return;
227         }
228
229         fastboot_okay(NULL, response);
230 }
231
232 /**
233  * fastboot_nand_flash_erase() - Erase NAND for fastboot
234  *
235  * @cmd: Named device to erase
236  * @response: Pointer to fastboot response buffer
237  */
238 void fastboot_nand_erase(const char *cmd, char *response)
239 {
240         struct part_info *part;
241         struct mtd_info *mtd = NULL;
242         int ret;
243
244         ret = fb_nand_lookup(cmd, &mtd, &part, response);
245         if (ret) {
246                 pr_err("invalid NAND device");
247                 fastboot_fail("invalid NAND device", response);
248                 return;
249         }
250
251         ret = board_fastboot_erase_partition_setup(part->name);
252         if (ret)
253                 return;
254
255         ret = _fb_nand_erase(mtd, part);
256         if (ret) {
257                 pr_err("failed erasing from device %s", mtd->name);
258                 fastboot_fail("failed erasing from device", response);
259                 return;
260         }
261
262         fastboot_okay(NULL, response);
263 }