Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2014 Broadcom Corporation. |
| 4 | * Copyright 2015 Free Electrons. |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <config.h> |
| 8 | #include <common.h> |
Simon Glass | e6f6f9e | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 9 | #include <blk.h> |
Simon Glass | b79fdc7 | 2020-05-10 11:39:54 -0600 | [diff] [blame] | 10 | #include <flash.h> |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 11 | |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 12 | #include <fastboot.h> |
Maxime Ripard | 3d4ef38 | 2015-10-15 14:34:19 +0200 | [diff] [blame] | 13 | #include <image-sparse.h> |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 14 | |
| 15 | #include <linux/mtd/mtd.h> |
| 16 | #include <jffs2/jffs2.h> |
| 17 | #include <nand.h> |
| 18 | |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 19 | struct fb_nand_sparse { |
Steve Rae | cc0f08cd | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 20 | struct mtd_info *mtd; |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 21 | struct part_info *part; |
| 22 | }; |
| 23 | |
Maxime Ripard | 6fb77c4 | 2015-10-15 14:34:18 +0200 | [diff] [blame] | 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 | |
Steve Rae | 9bc3479 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 34 | static int fb_nand_lookup(const char *partname, |
Sergey Kubushyn | 6171757 | 2016-06-07 14:22:59 -0700 | [diff] [blame] | 35 | struct mtd_info **mtd, |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 36 | struct part_info **part, |
| 37 | char *response) |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 38 | { |
| 39 | struct mtd_device *dev; |
| 40 | int ret; |
| 41 | u8 pnum; |
| 42 | |
| 43 | ret = mtdparts_init(); |
| 44 | if (ret) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 45 | pr_err("Cannot initialize MTD partitions\n"); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 46 | fastboot_fail("cannot init mtdparts", response); |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 47 | return ret; |
| 48 | } |
| 49 | |
| 50 | ret = find_dev_and_part(partname, &dev, &pnum, part); |
| 51 | if (ret) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 52 | pr_err("cannot find partition: '%s'", partname); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 53 | fastboot_fail("cannot find partition", response); |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 54 | return ret; |
| 55 | } |
| 56 | |
| 57 | if (dev->id->type != MTD_DEV_TYPE_NAND) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 58 | pr_err("partition '%s' is not stored on a NAND device", |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 59 | partname); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 60 | fastboot_fail("not a NAND device", response); |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 61 | return -EINVAL; |
| 62 | } |
| 63 | |
Grygorii Strashko | edba8cc | 2017-06-26 19:12:56 -0500 | [diff] [blame] | 64 | *mtd = get_nand_dev_by_index(dev->id->num); |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 69 | static int _fb_nand_erase(struct mtd_info *mtd, struct part_info *part) |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 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 | |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 82 | ret = nand_erase_opts(mtd, &opts); |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 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 | |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 92 | static int _fb_nand_write(struct mtd_info *mtd, struct part_info *part, |
Alex Kiernan | f73a7df | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 93 | void *buffer, u32 offset, |
Alex Kiernan | 52fdf10 | 2018-05-29 15:30:45 +0000 | [diff] [blame] | 94 | size_t length, size_t *written) |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 95 | { |
| 96 | int flags = WITH_WR_VERIFY; |
| 97 | |
| 98 | #ifdef CONFIG_FASTBOOT_FLASH_NAND_TRIMFFS |
| 99 | flags |= WITH_DROP_FFS; |
| 100 | #endif |
| 101 | |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 102 | return nand_write_skip_bad(mtd, offset, &length, written, |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 103 | part->size - (offset - part->offset), |
| 104 | buffer, flags); |
| 105 | } |
| 106 | |
Steve Rae | cc0f08cd | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 107 | static lbaint_t fb_nand_sparse_write(struct sparse_storage *info, |
| 108 | lbaint_t blk, lbaint_t blkcnt, const void *buffer) |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 109 | { |
Steve Rae | cc0f08cd | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 110 | struct fb_nand_sparse *sparse = info->priv; |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 111 | size_t written; |
| 112 | int ret; |
| 113 | |
Steve Rae | cc0f08cd | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 114 | ret = _fb_nand_write(sparse->mtd, sparse->part, (void *)buffer, |
| 115 | blk * info->blksz, |
| 116 | blkcnt * info->blksz, &written); |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 117 | if (ret < 0) { |
| 118 | printf("Failed to write sparse chunk\n"); |
| 119 | return ret; |
| 120 | } |
| 121 | |
Steve Rae | cc0f08cd | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 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; |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Steve Rae | 2c72404 | 2016-06-07 11:19:38 -0700 | [diff] [blame] | 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 | |
Alex Kiernan | d1a119d | 2018-05-29 15:30:48 +0000 | [diff] [blame] | 150 | /** |
Alex Kiernan | f73a7df | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 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 | */ |
Sam Protsenko | cacb03e | 2019-06-13 21:11:07 +0300 | [diff] [blame] | 157 | int fastboot_nand_get_part_info(const char *part_name, |
| 158 | struct part_info **part_info, char *response) |
Alex Kiernan | f73a7df | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 159 | { |
| 160 | struct mtd_info *mtd = NULL; |
| 161 | |
| 162 | return fb_nand_lookup(part_name, &mtd, part_info, response); |
| 163 | } |
| 164 | |
| 165 | /** |
Alex Kiernan | d1a119d | 2018-05-29 15:30:48 +0000 | [diff] [blame] | 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, |
Alex Kiernan | f73a7df | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 174 | u32 download_bytes, char *response) |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 175 | { |
| 176 | struct part_info *part; |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 177 | struct mtd_info *mtd = NULL; |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 178 | int ret; |
| 179 | |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 180 | ret = fb_nand_lookup(cmd, &mtd, &part, response); |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 181 | if (ret) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 182 | pr_err("invalid NAND device"); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 183 | fastboot_fail("invalid NAND device", response); |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | |
Maxime Ripard | 6fb77c4 | 2015-10-15 14:34:18 +0200 | [diff] [blame] | 187 | ret = board_fastboot_write_partition_setup(part->name); |
| 188 | if (ret) |
| 189 | return; |
| 190 | |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 191 | if (is_sparse_image(download_buffer)) { |
| 192 | struct fb_nand_sparse sparse_priv; |
Steve Rae | cc0f08cd | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 193 | struct sparse_storage sparse; |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 194 | |
Steve Rae | cc0f08cd | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 195 | sparse_priv.mtd = mtd; |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 196 | sparse_priv.part = part; |
| 197 | |
Steve Rae | cc0f08cd | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 198 | sparse.blksz = mtd->writesize; |
| 199 | sparse.start = part->offset / sparse.blksz; |
| 200 | sparse.size = part->size / sparse.blksz; |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 201 | sparse.write = fb_nand_sparse_write; |
Steve Rae | 2c72404 | 2016-06-07 11:19:38 -0700 | [diff] [blame] | 202 | sparse.reserve = fb_nand_sparse_reserve; |
Jassi Brar | 2f83f21 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 203 | sparse.mssg = fastboot_fail; |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 204 | |
Steve Rae | cc0f08cd | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 205 | printf("Flashing sparse image at offset " LBAFU "\n", |
| 206 | sparse.start); |
| 207 | |
| 208 | sparse.priv = &sparse_priv; |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 209 | ret = write_sparse_image(&sparse, cmd, download_buffer, |
| 210 | response); |
Jassi Brar | 2f83f21 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 211 | if (!ret) |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 212 | fastboot_okay(NULL, response); |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 213 | } else { |
| 214 | printf("Flashing raw image at offset 0x%llx\n", |
| 215 | part->offset); |
| 216 | |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 217 | ret = _fb_nand_write(mtd, part, download_buffer, part->offset, |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 218 | download_bytes, NULL); |
| 219 | |
| 220 | printf("........ wrote %u bytes to '%s'\n", |
| 221 | download_bytes, part->name); |
| 222 | } |
| 223 | |
| 224 | if (ret) { |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 225 | fastboot_fail("error writing the image", response); |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 226 | return; |
| 227 | } |
| 228 | |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 229 | fastboot_okay(NULL, response); |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Alex Kiernan | d1a119d | 2018-05-29 15:30:48 +0000 | [diff] [blame] | 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) |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 239 | { |
| 240 | struct part_info *part; |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 241 | struct mtd_info *mtd = NULL; |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 242 | int ret; |
| 243 | |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 244 | ret = fb_nand_lookup(cmd, &mtd, &part, response); |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 245 | if (ret) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 246 | pr_err("invalid NAND device"); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 247 | fastboot_fail("invalid NAND device", response); |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 248 | return; |
| 249 | } |
| 250 | |
Maxime Ripard | 6fb77c4 | 2015-10-15 14:34:18 +0200 | [diff] [blame] | 251 | ret = board_fastboot_erase_partition_setup(part->name); |
| 252 | if (ret) |
| 253 | return; |
| 254 | |
Scott Wood | 151c06e | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 255 | ret = _fb_nand_erase(mtd, part); |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 256 | if (ret) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 257 | pr_err("failed erasing from device %s", mtd->name); |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 258 | fastboot_fail("failed erasing from device", response); |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 259 | return; |
| 260 | } |
| 261 | |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 262 | fastboot_okay(NULL, response); |
Maxime Ripard | bf8940d | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 263 | } |