blob: c07655e49ed8ec10d6143b165855f70b55494d55 [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,
Maxime Ripard33602a92015-10-15 14:34:17 +020034 struct part_info **part)
35{
36 struct mtd_device *dev;
37 int ret;
38 u8 pnum;
39
40 ret = mtdparts_init();
41 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090042 pr_err("Cannot initialize MTD partitions\n");
Steve Rae76336b22016-06-07 11:19:37 -070043 fastboot_fail("cannot init mtdparts");
Maxime Ripard33602a92015-10-15 14:34:17 +020044 return ret;
45 }
46
47 ret = find_dev_and_part(partname, &dev, &pnum, part);
48 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090049 pr_err("cannot find partition: '%s'", partname);
Steve Rae76336b22016-06-07 11:19:37 -070050 fastboot_fail("cannot find partition");
Maxime Ripard33602a92015-10-15 14:34:17 +020051 return ret;
52 }
53
54 if (dev->id->type != MTD_DEV_TYPE_NAND) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090055 pr_err("partition '%s' is not stored on a NAND device",
Maxime Ripard33602a92015-10-15 14:34:17 +020056 partname);
Steve Rae76336b22016-06-07 11:19:37 -070057 fastboot_fail("not a NAND device");
Maxime Ripard33602a92015-10-15 14:34:17 +020058 return -EINVAL;
59 }
60
Grygorii Strashko5f33b822017-06-26 19:12:56 -050061 *mtd = get_nand_dev_by_index(dev->id->num);
Maxime Ripard33602a92015-10-15 14:34:17 +020062
63 return 0;
64}
65
Scott Wood08364d92016-05-30 13:57:54 -050066static int _fb_nand_erase(struct mtd_info *mtd, struct part_info *part)
Maxime Ripard33602a92015-10-15 14:34:17 +020067{
68 nand_erase_options_t opts;
69 int ret;
70
71 memset(&opts, 0, sizeof(opts));
72 opts.offset = part->offset;
73 opts.length = part->size;
74 opts.quiet = 1;
75
76 printf("Erasing blocks 0x%llx to 0x%llx\n",
77 part->offset, part->offset + part->size);
78
Scott Wood08364d92016-05-30 13:57:54 -050079 ret = nand_erase_opts(mtd, &opts);
Maxime Ripard33602a92015-10-15 14:34:17 +020080 if (ret)
81 return ret;
82
83 printf("........ erased 0x%llx bytes from '%s'\n",
84 part->size, part->name);
85
86 return 0;
87}
88
Scott Wood08364d92016-05-30 13:57:54 -050089static int _fb_nand_write(struct mtd_info *mtd, struct part_info *part,
Maxime Ripard33602a92015-10-15 14:34:17 +020090 void *buffer, unsigned int offset,
91 unsigned int length, size_t *written)
92{
93 int flags = WITH_WR_VERIFY;
94
95#ifdef CONFIG_FASTBOOT_FLASH_NAND_TRIMFFS
96 flags |= WITH_DROP_FFS;
97#endif
98
Scott Wood08364d92016-05-30 13:57:54 -050099 return nand_write_skip_bad(mtd, offset, &length, written,
Maxime Ripard33602a92015-10-15 14:34:17 +0200100 part->size - (offset - part->offset),
101 buffer, flags);
102}
103
Steve Rae8cf58db2016-06-07 11:19:36 -0700104static lbaint_t fb_nand_sparse_write(struct sparse_storage *info,
105 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
Maxime Ripard33602a92015-10-15 14:34:17 +0200106{
Steve Rae8cf58db2016-06-07 11:19:36 -0700107 struct fb_nand_sparse *sparse = info->priv;
Maxime Ripard33602a92015-10-15 14:34:17 +0200108 size_t written;
109 int ret;
110
Steve Rae8cf58db2016-06-07 11:19:36 -0700111 ret = _fb_nand_write(sparse->mtd, sparse->part, (void *)buffer,
112 blk * info->blksz,
113 blkcnt * info->blksz, &written);
Maxime Ripard33602a92015-10-15 14:34:17 +0200114 if (ret < 0) {
115 printf("Failed to write sparse chunk\n");
116 return ret;
117 }
118
Steve Rae8cf58db2016-06-07 11:19:36 -0700119/* TODO - verify that the value "written" includes the "bad-blocks" ... */
120
121 /*
122 * the return value must be 'blkcnt' ("good-blocks") plus the
123 * number of "bad-blocks" encountered within this space...
124 */
125 return written / info->blksz;
Maxime Ripard33602a92015-10-15 14:34:17 +0200126}
127
Steve Rae8a99be82016-06-07 11:19:38 -0700128static lbaint_t fb_nand_sparse_reserve(struct sparse_storage *info,
129 lbaint_t blk, lbaint_t blkcnt)
130{
131 int bad_blocks = 0;
132
133/*
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 )
138 */
139
140 /*
141 * the return value must be 'blkcnt' ("good-blocks") plus the
142 * number of "bad-blocks" encountered within this space...
143 */
144 return blkcnt + bad_blocks;
145}
146
Steve Rae8cf58db2016-06-07 11:19:36 -0700147void fb_nand_flash_write(const char *cmd, void *download_buffer,
Steve Rae76336b22016-06-07 11:19:37 -0700148 unsigned int download_bytes)
Maxime Ripard33602a92015-10-15 14:34:17 +0200149{
150 struct part_info *part;
Scott Wood08364d92016-05-30 13:57:54 -0500151 struct mtd_info *mtd = NULL;
Maxime Ripard33602a92015-10-15 14:34:17 +0200152 int ret;
153
Steve Rae76336b22016-06-07 11:19:37 -0700154 ret = fb_nand_lookup(cmd, &mtd, &part);
Maxime Ripard33602a92015-10-15 14:34:17 +0200155 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900156 pr_err("invalid NAND device");
Steve Rae76336b22016-06-07 11:19:37 -0700157 fastboot_fail("invalid NAND device");
Maxime Ripard33602a92015-10-15 14:34:17 +0200158 return;
159 }
160
Maxime Ripard18123542015-10-15 14:34:18 +0200161 ret = board_fastboot_write_partition_setup(part->name);
162 if (ret)
163 return;
164
Maxime Ripard33602a92015-10-15 14:34:17 +0200165 if (is_sparse_image(download_buffer)) {
166 struct fb_nand_sparse sparse_priv;
Steve Rae8cf58db2016-06-07 11:19:36 -0700167 struct sparse_storage sparse;
Maxime Ripard33602a92015-10-15 14:34:17 +0200168
Steve Rae8cf58db2016-06-07 11:19:36 -0700169 sparse_priv.mtd = mtd;
Maxime Ripard33602a92015-10-15 14:34:17 +0200170 sparse_priv.part = part;
171
Steve Rae8cf58db2016-06-07 11:19:36 -0700172 sparse.blksz = mtd->writesize;
173 sparse.start = part->offset / sparse.blksz;
174 sparse.size = part->size / sparse.blksz;
Maxime Ripard33602a92015-10-15 14:34:17 +0200175 sparse.write = fb_nand_sparse_write;
Steve Rae8a99be82016-06-07 11:19:38 -0700176 sparse.reserve = fb_nand_sparse_reserve;
Jassi Brar427e6692018-04-06 12:05:09 +0530177 sparse.mssg = fastboot_fail;
Maxime Ripard33602a92015-10-15 14:34:17 +0200178
Steve Rae8cf58db2016-06-07 11:19:36 -0700179 printf("Flashing sparse image at offset " LBAFU "\n",
180 sparse.start);
181
182 sparse.priv = &sparse_priv;
Jassi Brar427e6692018-04-06 12:05:09 +0530183 ret = write_sparse_image(&sparse, cmd, download_buffer);
184 if (!ret)
185 fastboot_okay("");
Maxime Ripard33602a92015-10-15 14:34:17 +0200186 } else {
187 printf("Flashing raw image at offset 0x%llx\n",
188 part->offset);
189
Scott Wood08364d92016-05-30 13:57:54 -0500190 ret = _fb_nand_write(mtd, part, download_buffer, part->offset,
Maxime Ripard33602a92015-10-15 14:34:17 +0200191 download_bytes, NULL);
192
193 printf("........ wrote %u bytes to '%s'\n",
194 download_bytes, part->name);
195 }
196
197 if (ret) {
Steve Rae76336b22016-06-07 11:19:37 -0700198 fastboot_fail("error writing the image");
Maxime Ripard33602a92015-10-15 14:34:17 +0200199 return;
200 }
201
Steve Rae76336b22016-06-07 11:19:37 -0700202 fastboot_okay("");
Maxime Ripard33602a92015-10-15 14:34:17 +0200203}
204
Steve Rae76336b22016-06-07 11:19:37 -0700205void fb_nand_erase(const char *cmd)
Maxime Ripard33602a92015-10-15 14:34:17 +0200206{
207 struct part_info *part;
Scott Wood08364d92016-05-30 13:57:54 -0500208 struct mtd_info *mtd = NULL;
Maxime Ripard33602a92015-10-15 14:34:17 +0200209 int ret;
210
Steve Rae76336b22016-06-07 11:19:37 -0700211 ret = fb_nand_lookup(cmd, &mtd, &part);
Maxime Ripard33602a92015-10-15 14:34:17 +0200212 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900213 pr_err("invalid NAND device");
Steve Rae76336b22016-06-07 11:19:37 -0700214 fastboot_fail("invalid NAND device");
Maxime Ripard33602a92015-10-15 14:34:17 +0200215 return;
216 }
217
Maxime Ripard18123542015-10-15 14:34:18 +0200218 ret = board_fastboot_erase_partition_setup(part->name);
219 if (ret)
220 return;
221
Scott Wood08364d92016-05-30 13:57:54 -0500222 ret = _fb_nand_erase(mtd, part);
Maxime Ripard33602a92015-10-15 14:34:17 +0200223 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900224 pr_err("failed erasing from device %s", mtd->name);
Steve Rae76336b22016-06-07 11:19:37 -0700225 fastboot_fail("failed erasing from device");
Maxime Ripard33602a92015-10-15 14:34:17 +0200226 return;
227 }
228
Steve Rae76336b22016-06-07 11:19:37 -0700229 fastboot_okay("");
Maxime Ripard33602a92015-10-15 14:34:17 +0200230}