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> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 8 | #include <blk.h> |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 9 | |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 10 | #include <fastboot.h> |
Maxime Ripard | 6af49bc | 2015-10-15 14:34:19 +0200 | [diff] [blame] | 11 | #include <image-sparse.h> |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 12 | |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 13 | #include <linux/printk.h> |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 14 | #include <linux/mtd/mtd.h> |
| 15 | #include <jffs2/jffs2.h> |
| 16 | #include <nand.h> |
| 17 | |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 18 | struct fb_nand_sparse { |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 19 | struct mtd_info *mtd; |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 20 | struct part_info *part; |
| 21 | }; |
| 22 | |
Maxime Ripard | 1812354 | 2015-10-15 14:34:18 +0200 | [diff] [blame] | 23 | __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 Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 33 | static int fb_nand_lookup(const char *partname, |
Sergey Kubushyn | 5ad7e83 | 2016-06-07 14:22:59 -0700 | [diff] [blame] | 34 | struct mtd_info **mtd, |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 35 | struct part_info **part, |
| 36 | char *response) |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 37 | { |
| 38 | struct mtd_device *dev; |
| 39 | int ret; |
| 40 | u8 pnum; |
| 41 | |
| 42 | ret = mtdparts_init(); |
| 43 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 44 | pr_err("Cannot initialize MTD partitions\n"); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 45 | fastboot_fail("cannot init mtdparts", response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 46 | return ret; |
| 47 | } |
| 48 | |
| 49 | ret = find_dev_and_part(partname, &dev, &pnum, part); |
| 50 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 51 | pr_err("cannot find partition: '%s'", partname); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 52 | fastboot_fail("cannot find partition", response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 53 | return ret; |
| 54 | } |
| 55 | |
| 56 | if (dev->id->type != MTD_DEV_TYPE_NAND) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 57 | pr_err("partition '%s' is not stored on a NAND device", |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 58 | partname); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 59 | fastboot_fail("not a NAND device", response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 60 | return -EINVAL; |
| 61 | } |
| 62 | |
Grygorii Strashko | 5f33b82 | 2017-06-26 19:12:56 -0500 | [diff] [blame] | 63 | *mtd = get_nand_dev_by_index(dev->id->num); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
Scott Wood | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 68 | 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] | 69 | { |
| 70 | nand_erase_options_t opts; |
| 71 | int ret; |
| 72 | |
| 73 | memset(&opts, 0, sizeof(opts)); |
| 74 | opts.offset = part->offset; |
| 75 | opts.length = part->size; |
| 76 | opts.quiet = 1; |
| 77 | |
| 78 | printf("Erasing blocks 0x%llx to 0x%llx\n", |
| 79 | part->offset, part->offset + part->size); |
| 80 | |
Scott Wood | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 81 | ret = nand_erase_opts(mtd, &opts); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 82 | if (ret) |
| 83 | return ret; |
| 84 | |
| 85 | printf("........ erased 0x%llx bytes from '%s'\n", |
| 86 | part->size, part->name); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
Scott Wood | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 91 | 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] | 92 | void *buffer, u32 offset, |
Alex Kiernan | d0f91f0 | 2018-05-29 15:30:45 +0000 | [diff] [blame] | 93 | size_t length, size_t *written) |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 94 | { |
| 95 | int flags = WITH_WR_VERIFY; |
| 96 | |
| 97 | #ifdef CONFIG_FASTBOOT_FLASH_NAND_TRIMFFS |
| 98 | flags |= WITH_DROP_FFS; |
| 99 | #endif |
| 100 | |
Scott Wood | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 101 | return nand_write_skip_bad(mtd, offset, &length, written, |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 102 | part->size - (offset - part->offset), |
| 103 | buffer, flags); |
| 104 | } |
| 105 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 106 | static lbaint_t fb_nand_sparse_write(struct sparse_storage *info, |
| 107 | lbaint_t blk, lbaint_t blkcnt, const void *buffer) |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 108 | { |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 109 | struct fb_nand_sparse *sparse = info->priv; |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 110 | size_t written; |
| 111 | int ret; |
| 112 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 113 | ret = _fb_nand_write(sparse->mtd, sparse->part, (void *)buffer, |
| 114 | blk * info->blksz, |
| 115 | blkcnt * info->blksz, &written); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 116 | if (ret < 0) { |
| 117 | printf("Failed to write sparse chunk\n"); |
| 118 | return ret; |
| 119 | } |
| 120 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 121 | /* TODO - verify that the value "written" includes the "bad-blocks" ... */ |
| 122 | |
| 123 | /* |
| 124 | * the return value must be 'blkcnt' ("good-blocks") plus the |
| 125 | * number of "bad-blocks" encountered within this space... |
| 126 | */ |
| 127 | return written / info->blksz; |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 128 | } |
| 129 | |
Steve Rae | 8a99be8 | 2016-06-07 11:19:38 -0700 | [diff] [blame] | 130 | static lbaint_t fb_nand_sparse_reserve(struct sparse_storage *info, |
| 131 | lbaint_t blk, lbaint_t blkcnt) |
| 132 | { |
| 133 | int bad_blocks = 0; |
| 134 | |
| 135 | /* |
| 136 | * TODO - implement a function to determine the total number |
| 137 | * of blocks which must be used in order to reserve the specified |
| 138 | * number ("blkcnt") of "good-blocks", starting at "blk"... |
| 139 | * ( possibly something like the "check_skip_len()" function ) |
| 140 | */ |
| 141 | |
| 142 | /* |
| 143 | * the return value must be 'blkcnt' ("good-blocks") plus the |
| 144 | * number of "bad-blocks" encountered within this space... |
| 145 | */ |
| 146 | return blkcnt + bad_blocks; |
| 147 | } |
| 148 | |
Alex Kiernan | 32c2194 | 2018-05-29 15:30:48 +0000 | [diff] [blame] | 149 | /** |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 150 | * fastboot_nand_get_part_info() - Lookup NAND partion by name |
| 151 | * |
| 152 | * @part_name: Named device to lookup |
| 153 | * @part_info: Pointer to returned part_info pointer |
| 154 | * @response: Pointer to fastboot response buffer |
| 155 | */ |
Sam Protsenko | 0fb37ee | 2019-06-13 21:11:07 +0300 | [diff] [blame] | 156 | int fastboot_nand_get_part_info(const char *part_name, |
| 157 | struct part_info **part_info, char *response) |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 158 | { |
| 159 | struct mtd_info *mtd = NULL; |
| 160 | |
| 161 | return fb_nand_lookup(part_name, &mtd, part_info, response); |
| 162 | } |
| 163 | |
| 164 | /** |
Alex Kiernan | 32c2194 | 2018-05-29 15:30:48 +0000 | [diff] [blame] | 165 | * fastboot_nand_flash_write() - Write image to NAND for fastboot |
| 166 | * |
| 167 | * @cmd: Named device to write image to |
| 168 | * @download_buffer: Pointer to image data |
| 169 | * @download_bytes: Size of image data |
| 170 | * @response: Pointer to fastboot response buffer |
| 171 | */ |
| 172 | void fastboot_nand_flash_write(const char *cmd, void *download_buffer, |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 173 | u32 download_bytes, char *response) |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 174 | { |
| 175 | struct part_info *part; |
Scott Wood | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 176 | struct mtd_info *mtd = NULL; |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 177 | int ret; |
| 178 | |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 179 | ret = fb_nand_lookup(cmd, &mtd, &part, response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 180 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 181 | pr_err("invalid NAND device"); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 182 | fastboot_fail("invalid NAND device", response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 183 | return; |
| 184 | } |
| 185 | |
Maxime Ripard | 1812354 | 2015-10-15 14:34:18 +0200 | [diff] [blame] | 186 | ret = board_fastboot_write_partition_setup(part->name); |
| 187 | if (ret) |
| 188 | return; |
| 189 | |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 190 | if (is_sparse_image(download_buffer)) { |
| 191 | struct fb_nand_sparse sparse_priv; |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 192 | struct sparse_storage sparse; |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 193 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 194 | sparse_priv.mtd = mtd; |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 195 | sparse_priv.part = part; |
| 196 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 197 | sparse.blksz = mtd->writesize; |
| 198 | sparse.start = part->offset / sparse.blksz; |
| 199 | sparse.size = part->size / sparse.blksz; |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 200 | sparse.write = fb_nand_sparse_write; |
Steve Rae | 8a99be8 | 2016-06-07 11:19:38 -0700 | [diff] [blame] | 201 | sparse.reserve = fb_nand_sparse_reserve; |
Jassi Brar | 427e669 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 202 | sparse.mssg = fastboot_fail; |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 203 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 204 | printf("Flashing sparse image at offset " LBAFU "\n", |
| 205 | sparse.start); |
| 206 | |
| 207 | sparse.priv = &sparse_priv; |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 208 | ret = write_sparse_image(&sparse, cmd, download_buffer, |
| 209 | response); |
Jassi Brar | 427e669 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 210 | if (!ret) |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 211 | fastboot_okay(NULL, response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 212 | } else { |
| 213 | printf("Flashing raw image at offset 0x%llx\n", |
| 214 | part->offset); |
| 215 | |
Scott Wood | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 216 | ret = _fb_nand_write(mtd, part, download_buffer, part->offset, |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 217 | download_bytes, NULL); |
| 218 | |
| 219 | printf("........ wrote %u bytes to '%s'\n", |
| 220 | download_bytes, part->name); |
| 221 | } |
| 222 | |
| 223 | if (ret) { |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 224 | fastboot_fail("error writing the image", response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 225 | return; |
| 226 | } |
| 227 | |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 228 | fastboot_okay(NULL, response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Alex Kiernan | 32c2194 | 2018-05-29 15:30:48 +0000 | [diff] [blame] | 231 | /** |
| 232 | * fastboot_nand_flash_erase() - Erase NAND for fastboot |
| 233 | * |
| 234 | * @cmd: Named device to erase |
| 235 | * @response: Pointer to fastboot response buffer |
| 236 | */ |
| 237 | void fastboot_nand_erase(const char *cmd, char *response) |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 238 | { |
| 239 | struct part_info *part; |
Scott Wood | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 240 | struct mtd_info *mtd = NULL; |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 241 | int ret; |
| 242 | |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 243 | ret = fb_nand_lookup(cmd, &mtd, &part, response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 244 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 245 | pr_err("invalid NAND device"); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 246 | fastboot_fail("invalid NAND device", response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 247 | return; |
| 248 | } |
| 249 | |
Maxime Ripard | 1812354 | 2015-10-15 14:34:18 +0200 | [diff] [blame] | 250 | ret = board_fastboot_erase_partition_setup(part->name); |
| 251 | if (ret) |
| 252 | return; |
| 253 | |
Scott Wood | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 254 | ret = _fb_nand_erase(mtd, part); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 255 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 256 | pr_err("failed erasing from device %s", mtd->name); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 257 | fastboot_fail("failed erasing from device", response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 258 | return; |
| 259 | } |
| 260 | |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 261 | fastboot_okay(NULL, response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 262 | } |