blob: ae34f4891b6e9fc55d78e42d8687af7586be780b [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#include <sparse_format.h>
14
15#include <linux/mtd/mtd.h>
16#include <jffs2/jffs2.h>
17#include <nand.h>
18
19static char *response_str;
20
21struct fb_nand_sparse {
Scott Wood151c06e2016-05-30 13:57:54 -050022 struct mtd_info *nand;
Maxime Ripardbf8940d2015-10-15 14:34:17 +020023 struct part_info *part;
24};
25
Maxime Ripard6fb77c42015-10-15 14:34:18 +020026__weak int board_fastboot_erase_partition_setup(char *name)
27{
28 return 0;
29}
30
31__weak int board_fastboot_write_partition_setup(char *name)
32{
33 return 0;
34}
35
Maxime Ripardbf8940d2015-10-15 14:34:17 +020036static int fb_nand_lookup(const char *partname, char *response,
Sergey Kubushyn61717572016-06-07 14:22:59 -070037 struct mtd_info **mtd,
Maxime Ripardbf8940d2015-10-15 14:34:17 +020038 struct part_info **part)
39{
40 struct mtd_device *dev;
41 int ret;
42 u8 pnum;
43
44 ret = mtdparts_init();
45 if (ret) {
46 error("Cannot initialize MTD partitions\n");
47 fastboot_fail(response_str, "cannot init mtdparts");
48 return ret;
49 }
50
51 ret = find_dev_and_part(partname, &dev, &pnum, part);
52 if (ret) {
53 error("cannot find partition: '%s'", partname);
54 fastboot_fail(response_str, "cannot find partition");
55 return ret;
56 }
57
58 if (dev->id->type != MTD_DEV_TYPE_NAND) {
59 error("partition '%s' is not stored on a NAND device",
60 partname);
61 fastboot_fail(response_str, "not a NAND device");
62 return -EINVAL;
63 }
64
Scott Woodb616d9b2016-05-30 13:57:55 -050065 *mtd = nand_info[dev->id->num];
Maxime Ripardbf8940d2015-10-15 14:34:17 +020066
67 return 0;
68}
69
Scott Wood151c06e2016-05-30 13:57:54 -050070static int _fb_nand_erase(struct mtd_info *mtd, struct part_info *part)
Maxime Ripardbf8940d2015-10-15 14:34:17 +020071{
72 nand_erase_options_t opts;
73 int ret;
74
75 memset(&opts, 0, sizeof(opts));
76 opts.offset = part->offset;
77 opts.length = part->size;
78 opts.quiet = 1;
79
80 printf("Erasing blocks 0x%llx to 0x%llx\n",
81 part->offset, part->offset + part->size);
82
Scott Wood151c06e2016-05-30 13:57:54 -050083 ret = nand_erase_opts(mtd, &opts);
Maxime Ripardbf8940d2015-10-15 14:34:17 +020084 if (ret)
85 return ret;
86
87 printf("........ erased 0x%llx bytes from '%s'\n",
88 part->size, part->name);
89
90 return 0;
91}
92
Scott Wood151c06e2016-05-30 13:57:54 -050093static int _fb_nand_write(struct mtd_info *mtd, struct part_info *part,
Maxime Ripardbf8940d2015-10-15 14:34:17 +020094 void *buffer, unsigned int offset,
95 unsigned int length, size_t *written)
96{
97 int flags = WITH_WR_VERIFY;
98
99#ifdef CONFIG_FASTBOOT_FLASH_NAND_TRIMFFS
100 flags |= WITH_DROP_FFS;
101#endif
102
Scott Wood151c06e2016-05-30 13:57:54 -0500103 return nand_write_skip_bad(mtd, offset, &length, written,
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200104 part->size - (offset - part->offset),
105 buffer, flags);
106}
107
108static int fb_nand_sparse_write(struct sparse_storage *storage,
109 void *priv,
110 unsigned int offset,
111 unsigned int size,
112 char *data)
113{
114 struct fb_nand_sparse *sparse = priv;
115 size_t written;
116 int ret;
117
118 ret = _fb_nand_write(sparse->nand, sparse->part, data,
119 offset * storage->block_sz,
120 size * storage->block_sz, &written);
121 if (ret < 0) {
122 printf("Failed to write sparse chunk\n");
123 return ret;
124 }
125
126 return written / storage->block_sz;
127}
128
129void fb_nand_flash_write(const char *partname, unsigned int session_id,
130 void *download_buffer, unsigned int download_bytes,
131 char *response)
132{
133 struct part_info *part;
Scott Wood151c06e2016-05-30 13:57:54 -0500134 struct mtd_info *mtd = NULL;
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200135 int ret;
136
137 /* initialize the response buffer */
138 response_str = response;
139
Scott Wood151c06e2016-05-30 13:57:54 -0500140 ret = fb_nand_lookup(partname, response, &mtd, &part);
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200141 if (ret) {
142 error("invalid NAND device");
143 fastboot_fail(response_str, "invalid NAND device");
144 return;
145 }
146
Maxime Ripard6fb77c42015-10-15 14:34:18 +0200147 ret = board_fastboot_write_partition_setup(part->name);
148 if (ret)
149 return;
150
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200151 if (is_sparse_image(download_buffer)) {
152 struct fb_nand_sparse sparse_priv;
153 sparse_storage_t sparse;
154
Scott Wood151c06e2016-05-30 13:57:54 -0500155 sparse_priv.nand = mtd;
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200156 sparse_priv.part = part;
157
Scott Wood151c06e2016-05-30 13:57:54 -0500158 sparse.block_sz = mtd->writesize;
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200159 sparse.start = part->offset / sparse.block_sz;
160 sparse.size = part->size / sparse.block_sz;
161 sparse.name = part->name;
162 sparse.write = fb_nand_sparse_write;
163
164 ret = store_sparse_image(&sparse, &sparse_priv, session_id,
165 download_buffer);
166 } else {
167 printf("Flashing raw image at offset 0x%llx\n",
168 part->offset);
169
Scott Wood151c06e2016-05-30 13:57:54 -0500170 ret = _fb_nand_write(mtd, part, download_buffer, part->offset,
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200171 download_bytes, NULL);
172
173 printf("........ wrote %u bytes to '%s'\n",
174 download_bytes, part->name);
175 }
176
177 if (ret) {
178 fastboot_fail(response_str, "error writing the image");
179 return;
180 }
181
182 fastboot_okay(response_str, "");
183}
184
185void fb_nand_erase(const char *partname, char *response)
186{
187 struct part_info *part;
Scott Wood151c06e2016-05-30 13:57:54 -0500188 struct mtd_info *mtd = NULL;
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200189 int ret;
190
191 /* initialize the response buffer */
192 response_str = response;
193
Scott Wood151c06e2016-05-30 13:57:54 -0500194 ret = fb_nand_lookup(partname, response, &mtd, &part);
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200195 if (ret) {
196 error("invalid NAND device");
197 fastboot_fail(response_str, "invalid NAND device");
198 return;
199 }
200
Maxime Ripard6fb77c42015-10-15 14:34:18 +0200201 ret = board_fastboot_erase_partition_setup(part->name);
202 if (ret)
203 return;
204
Scott Wood151c06e2016-05-30 13:57:54 -0500205 ret = _fb_nand_erase(mtd, part);
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200206 if (ret) {
Scott Wood151c06e2016-05-30 13:57:54 -0500207 error("failed erasing from device %s", mtd->name);
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200208 fastboot_fail(response_str, "failed erasing from device");
209 return;
210 }
211
212 fastboot_okay(response_str, "");
213}