blob: 3d027d43755486dcf53cdc31dbc728ba4191f33c [file] [log] [blame]
Maxime Ripardbf8940d2015-10-15 14:34:17 +02001/*
2 * Copyright 2014 Broadcom Corporation.
3 * Copyright 2015 Free Electrons.
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <config.h>
9#include <common.h>
10
Maxime Ripardbf8940d2015-10-15 14:34:17 +020011#include <fastboot.h>
Maxime Ripard3d4ef382015-10-15 14:34:19 +020012#include <image-sparse.h>
Maxime Ripardbf8940d2015-10-15 14:34:17 +020013
14#include <linux/mtd/mtd.h>
15#include <jffs2/jffs2.h>
16#include <nand.h>
17
Maxime Ripardbf8940d2015-10-15 14:34:17 +020018struct fb_nand_sparse {
Steve Raecc0f08cd2016-06-07 11:19:36 -070019 struct mtd_info *mtd;
Maxime Ripardbf8940d2015-10-15 14:34:17 +020020 struct part_info *part;
21};
22
Maxime Ripard6fb77c42015-10-15 14:34:18 +020023__weak int board_fastboot_erase_partition_setup(char *name)
24{
25 return 0;
26}
27
28__weak int board_fastboot_write_partition_setup(char *name)
29{
30 return 0;
31}
32
Steve Rae9bc34792016-06-07 11:19:37 -070033static int fb_nand_lookup(const char *partname,
Sergey Kubushyn61717572016-06-07 14:22:59 -070034 struct mtd_info **mtd,
Maxime Ripardbf8940d2015-10-15 14:34:17 +020035 struct part_info **part)
36{
37 struct mtd_device *dev;
38 int ret;
39 u8 pnum;
40
41 ret = mtdparts_init();
42 if (ret) {
43 error("Cannot initialize MTD partitions\n");
Steve Rae9bc34792016-06-07 11:19:37 -070044 fastboot_fail("cannot init mtdparts");
Maxime Ripardbf8940d2015-10-15 14:34:17 +020045 return ret;
46 }
47
48 ret = find_dev_and_part(partname, &dev, &pnum, part);
49 if (ret) {
50 error("cannot find partition: '%s'", partname);
Steve Rae9bc34792016-06-07 11:19:37 -070051 fastboot_fail("cannot find partition");
Maxime Ripardbf8940d2015-10-15 14:34:17 +020052 return ret;
53 }
54
55 if (dev->id->type != MTD_DEV_TYPE_NAND) {
56 error("partition '%s' is not stored on a NAND device",
57 partname);
Steve Rae9bc34792016-06-07 11:19:37 -070058 fastboot_fail("not a NAND device");
Maxime Ripardbf8940d2015-10-15 14:34:17 +020059 return -EINVAL;
60 }
61
Grygorii Strashkoedba8cc2017-06-26 19:12:56 -050062 *mtd = get_nand_dev_by_index(dev->id->num);
Maxime Ripardbf8940d2015-10-15 14:34:17 +020063
64 return 0;
65}
66
Scott Wood151c06e2016-05-30 13:57:54 -050067static int _fb_nand_erase(struct mtd_info *mtd, struct part_info *part)
Maxime Ripardbf8940d2015-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 Wood151c06e2016-05-30 13:57:54 -050080 ret = nand_erase_opts(mtd, &opts);
Maxime Ripardbf8940d2015-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 Wood151c06e2016-05-30 13:57:54 -050090static int _fb_nand_write(struct mtd_info *mtd, struct part_info *part,
Maxime Ripardbf8940d2015-10-15 14:34:17 +020091 void *buffer, unsigned int offset,
92 unsigned int length, size_t *written)
93{
94 int flags = WITH_WR_VERIFY;
95
96#ifdef CONFIG_FASTBOOT_FLASH_NAND_TRIMFFS
97 flags |= WITH_DROP_FFS;
98#endif
99
Scott Wood151c06e2016-05-30 13:57:54 -0500100 return nand_write_skip_bad(mtd, offset, &length, written,
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200101 part->size - (offset - part->offset),
102 buffer, flags);
103}
104
Steve Raecc0f08cd2016-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 Ripardbf8940d2015-10-15 14:34:17 +0200107{
Steve Raecc0f08cd2016-06-07 11:19:36 -0700108 struct fb_nand_sparse *sparse = info->priv;
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200109 size_t written;
110 int ret;
111
Steve Raecc0f08cd2016-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 Ripardbf8940d2015-10-15 14:34:17 +0200115 if (ret < 0) {
116 printf("Failed to write sparse chunk\n");
117 return ret;
118 }
119
Steve Raecc0f08cd2016-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 Ripardbf8940d2015-10-15 14:34:17 +0200127}
128
Steve Rae2c724042016-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
Steve Raecc0f08cd2016-06-07 11:19:36 -0700148void fb_nand_flash_write(const char *cmd, void *download_buffer,
Steve Rae9bc34792016-06-07 11:19:37 -0700149 unsigned int download_bytes)
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200150{
151 struct part_info *part;
Scott Wood151c06e2016-05-30 13:57:54 -0500152 struct mtd_info *mtd = NULL;
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200153 int ret;
154
Steve Rae9bc34792016-06-07 11:19:37 -0700155 ret = fb_nand_lookup(cmd, &mtd, &part);
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200156 if (ret) {
157 error("invalid NAND device");
Steve Rae9bc34792016-06-07 11:19:37 -0700158 fastboot_fail("invalid NAND device");
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200159 return;
160 }
161
Maxime Ripard6fb77c42015-10-15 14:34:18 +0200162 ret = board_fastboot_write_partition_setup(part->name);
163 if (ret)
164 return;
165
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200166 if (is_sparse_image(download_buffer)) {
167 struct fb_nand_sparse sparse_priv;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700168 struct sparse_storage sparse;
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200169
Steve Raecc0f08cd2016-06-07 11:19:36 -0700170 sparse_priv.mtd = mtd;
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200171 sparse_priv.part = part;
172
Steve Raecc0f08cd2016-06-07 11:19:36 -0700173 sparse.blksz = mtd->writesize;
174 sparse.start = part->offset / sparse.blksz;
175 sparse.size = part->size / sparse.blksz;
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200176 sparse.write = fb_nand_sparse_write;
Steve Rae2c724042016-06-07 11:19:38 -0700177 sparse.reserve = fb_nand_sparse_reserve;
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200178
Steve Raecc0f08cd2016-06-07 11:19:36 -0700179 printf("Flashing sparse image at offset " LBAFU "\n",
180 sparse.start);
181
182 sparse.priv = &sparse_priv;
183 write_sparse_image(&sparse, cmd, download_buffer,
Steve Rae9bc34792016-06-07 11:19:37 -0700184 download_bytes);
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200185 } else {
186 printf("Flashing raw image at offset 0x%llx\n",
187 part->offset);
188
Scott Wood151c06e2016-05-30 13:57:54 -0500189 ret = _fb_nand_write(mtd, part, download_buffer, part->offset,
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200190 download_bytes, NULL);
191
192 printf("........ wrote %u bytes to '%s'\n",
193 download_bytes, part->name);
194 }
195
196 if (ret) {
Steve Rae9bc34792016-06-07 11:19:37 -0700197 fastboot_fail("error writing the image");
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200198 return;
199 }
200
Steve Rae9bc34792016-06-07 11:19:37 -0700201 fastboot_okay("");
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200202}
203
Steve Rae9bc34792016-06-07 11:19:37 -0700204void fb_nand_erase(const char *cmd)
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200205{
206 struct part_info *part;
Scott Wood151c06e2016-05-30 13:57:54 -0500207 struct mtd_info *mtd = NULL;
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200208 int ret;
209
Steve Rae9bc34792016-06-07 11:19:37 -0700210 ret = fb_nand_lookup(cmd, &mtd, &part);
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200211 if (ret) {
212 error("invalid NAND device");
Steve Rae9bc34792016-06-07 11:19:37 -0700213 fastboot_fail("invalid NAND device");
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200214 return;
215 }
216
Maxime Ripard6fb77c42015-10-15 14:34:18 +0200217 ret = board_fastboot_erase_partition_setup(part->name);
218 if (ret)
219 return;
220
Scott Wood151c06e2016-05-30 13:57:54 -0500221 ret = _fb_nand_erase(mtd, part);
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200222 if (ret) {
Scott Wood151c06e2016-05-30 13:57:54 -0500223 error("failed erasing from device %s", mtd->name);
Steve Rae9bc34792016-06-07 11:19:37 -0700224 fastboot_fail("failed erasing from device");
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200225 return;
226 }
227
Steve Rae9bc34792016-06-07 11:19:37 -0700228 fastboot_okay("");
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200229}