Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2014 Broadcom Corporation. |
| 4 | * Copyright 2015 Free Electrons. |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <config.h> |
| 8 | #include <common.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 9 | #include <blk.h> |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 10 | |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 11 | #include <fastboot.h> |
Maxime Ripard | 6af49bc | 2015-10-15 14:34:19 +0200 | [diff] [blame] | 12 | #include <image-sparse.h> |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 13 | |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 14 | #include <linux/printk.h> |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 15 | #include <linux/mtd/mtd.h> |
| 16 | #include <jffs2/jffs2.h> |
| 17 | #include <nand.h> |
| 18 | |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 19 | struct fb_nand_sparse { |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 20 | struct mtd_info *mtd; |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 21 | struct part_info *part; |
| 22 | }; |
| 23 | |
Maxime Ripard | 1812354 | 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 | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 34 | static int fb_nand_lookup(const char *partname, |
Sergey Kubushyn | 5ad7e83 | 2016-06-07 14:22:59 -0700 | [diff] [blame] | 35 | struct mtd_info **mtd, |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 36 | struct part_info **part, |
| 37 | char *response) |
Maxime Ripard | 33602a9 | 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 | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 45 | pr_err("Cannot initialize MTD partitions\n"); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 46 | fastboot_fail("cannot init mtdparts", response); |
Maxime Ripard | 33602a9 | 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 | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 52 | pr_err("cannot find partition: '%s'", partname); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 53 | fastboot_fail("cannot find partition", response); |
Maxime Ripard | 33602a9 | 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 | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 58 | pr_err("partition '%s' is not stored on a NAND device", |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 59 | partname); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 60 | fastboot_fail("not a NAND device", response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 61 | return -EINVAL; |
| 62 | } |
| 63 | |
Grygorii Strashko | 5f33b82 | 2017-06-26 19:12:56 -0500 | [diff] [blame] | 64 | *mtd = get_nand_dev_by_index(dev->id->num); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
Scott Wood | 08364d9 | 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 | 33602a9 | 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 | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 82 | ret = nand_erase_opts(mtd, &opts); |
Maxime Ripard | 33602a9 | 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 | 08364d9 | 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 | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 93 | void *buffer, u32 offset, |
Alex Kiernan | d0f91f0 | 2018-05-29 15:30:45 +0000 | [diff] [blame] | 94 | size_t length, size_t *written) |
Maxime Ripard | 33602a9 | 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 | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 102 | return nand_write_skip_bad(mtd, offset, &length, written, |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 103 | part->size - (offset - part->offset), |
| 104 | buffer, flags); |
| 105 | } |
| 106 | |
Steve Rae | 8cf58db | 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 | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 109 | { |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 110 | struct fb_nand_sparse *sparse = info->priv; |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 111 | size_t written; |
| 112 | int ret; |
| 113 | |
Steve Rae | 8cf58db | 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 | 33602a9 | 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 | 8cf58db | 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 | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Steve Rae | 8a99be8 | 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 | 32c2194 | 2018-05-29 15:30:48 +0000 | [diff] [blame] | 150 | /** |
Alex Kiernan | d5aa57c | 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 | 0fb37ee | 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 | d5aa57c | 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 | 32c2194 | 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 | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 174 | u32 download_bytes, char *response) |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 175 | { |
| 176 | struct part_info *part; |
Scott Wood | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 177 | struct mtd_info *mtd = NULL; |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 178 | int ret; |
| 179 | |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 180 | ret = fb_nand_lookup(cmd, &mtd, &part, response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 181 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 182 | pr_err("invalid NAND device"); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 183 | fastboot_fail("invalid NAND device", response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | |
Maxime Ripard | 1812354 | 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 | 33602a9 | 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 | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 193 | struct sparse_storage sparse; |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 194 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 195 | sparse_priv.mtd = mtd; |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 196 | sparse_priv.part = part; |
| 197 | |
Steve Rae | 8cf58db | 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 | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 201 | sparse.write = fb_nand_sparse_write; |
Steve Rae | 8a99be8 | 2016-06-07 11:19:38 -0700 | [diff] [blame] | 202 | sparse.reserve = fb_nand_sparse_reserve; |
Jassi Brar | 427e669 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 203 | sparse.mssg = fastboot_fail; |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 204 | |
Steve Rae | 8cf58db | 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 | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 209 | ret = write_sparse_image(&sparse, cmd, download_buffer, |
| 210 | response); |
Jassi Brar | 427e669 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 211 | if (!ret) |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 212 | fastboot_okay(NULL, response); |
Maxime Ripard | 33602a9 | 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 | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 217 | ret = _fb_nand_write(mtd, part, download_buffer, part->offset, |
Maxime Ripard | 33602a9 | 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 | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 225 | fastboot_fail("error writing the image", response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 226 | return; |
| 227 | } |
| 228 | |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 229 | fastboot_okay(NULL, response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Alex Kiernan | 32c2194 | 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 | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 239 | { |
| 240 | struct part_info *part; |
Scott Wood | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 241 | struct mtd_info *mtd = NULL; |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 242 | int ret; |
| 243 | |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 244 | ret = fb_nand_lookup(cmd, &mtd, &part, response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 245 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 246 | pr_err("invalid NAND device"); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 247 | fastboot_fail("invalid NAND device", response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 248 | return; |
| 249 | } |
| 250 | |
Maxime Ripard | 1812354 | 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 | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 255 | ret = _fb_nand_erase(mtd, part); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 256 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 257 | pr_err("failed erasing from device %s", mtd->name); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 258 | fastboot_fail("failed erasing from device", response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 259 | return; |
| 260 | } |
| 261 | |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 262 | fastboot_okay(NULL, response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 263 | } |