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> |
| 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 | |
| 13 | #include <linux/mtd/mtd.h> |
| 14 | #include <jffs2/jffs2.h> |
| 15 | #include <nand.h> |
| 16 | |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 17 | struct fb_nand_sparse { |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 18 | struct mtd_info *mtd; |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 19 | struct part_info *part; |
| 20 | }; |
| 21 | |
Maxime Ripard | 1812354 | 2015-10-15 14:34:18 +0200 | [diff] [blame] | 22 | __weak int board_fastboot_erase_partition_setup(char *name) |
| 23 | { |
| 24 | return 0; |
| 25 | } |
| 26 | |
| 27 | __weak int board_fastboot_write_partition_setup(char *name) |
| 28 | { |
| 29 | return 0; |
| 30 | } |
| 31 | |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 32 | static int fb_nand_lookup(const char *partname, |
Sergey Kubushyn | 5ad7e83 | 2016-06-07 14:22:59 -0700 | [diff] [blame] | 33 | struct mtd_info **mtd, |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 34 | struct part_info **part, |
| 35 | char *response) |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 36 | { |
| 37 | struct mtd_device *dev; |
| 38 | int ret; |
| 39 | u8 pnum; |
| 40 | |
| 41 | ret = mtdparts_init(); |
| 42 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 43 | pr_err("Cannot initialize MTD partitions\n"); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 44 | fastboot_fail("cannot init mtdparts", response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 45 | return ret; |
| 46 | } |
| 47 | |
| 48 | ret = find_dev_and_part(partname, &dev, &pnum, part); |
| 49 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 50 | pr_err("cannot find partition: '%s'", partname); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 51 | fastboot_fail("cannot find partition", response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 52 | return ret; |
| 53 | } |
| 54 | |
| 55 | if (dev->id->type != MTD_DEV_TYPE_NAND) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 56 | pr_err("partition '%s' is not stored on a NAND device", |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 57 | partname); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 58 | fastboot_fail("not a NAND device", response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 59 | return -EINVAL; |
| 60 | } |
| 61 | |
Grygorii Strashko | 5f33b82 | 2017-06-26 19:12:56 -0500 | [diff] [blame] | 62 | *mtd = get_nand_dev_by_index(dev->id->num); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
Scott Wood | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 67 | 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] | 68 | { |
| 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 Wood | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 80 | ret = nand_erase_opts(mtd, &opts); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 81 | 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 Wood | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 90 | static int _fb_nand_write(struct mtd_info *mtd, struct part_info *part, |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 91 | void *buffer, unsigned int offset, |
Alex Kiernan | d0f91f0 | 2018-05-29 15:30:45 +0000 | [diff] [blame] | 92 | size_t length, size_t *written) |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 93 | { |
| 94 | int flags = WITH_WR_VERIFY; |
| 95 | |
| 96 | #ifdef CONFIG_FASTBOOT_FLASH_NAND_TRIMFFS |
| 97 | flags |= WITH_DROP_FFS; |
| 98 | #endif |
| 99 | |
Scott Wood | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 100 | return nand_write_skip_bad(mtd, offset, &length, written, |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 101 | part->size - (offset - part->offset), |
| 102 | buffer, flags); |
| 103 | } |
| 104 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 105 | static lbaint_t fb_nand_sparse_write(struct sparse_storage *info, |
| 106 | lbaint_t blk, lbaint_t blkcnt, const void *buffer) |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 107 | { |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 108 | struct fb_nand_sparse *sparse = info->priv; |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 109 | size_t written; |
| 110 | int ret; |
| 111 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 112 | ret = _fb_nand_write(sparse->mtd, sparse->part, (void *)buffer, |
| 113 | blk * info->blksz, |
| 114 | blkcnt * info->blksz, &written); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 115 | if (ret < 0) { |
| 116 | printf("Failed to write sparse chunk\n"); |
| 117 | return ret; |
| 118 | } |
| 119 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 120 | /* 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 Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Steve Rae | 8a99be8 | 2016-06-07 11:19:38 -0700 | [diff] [blame] | 129 | static 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 | |
Alex Kiernan | 32c2194 | 2018-05-29 15:30:48 +0000 | [diff] [blame^] | 148 | /** |
| 149 | * fastboot_nand_flash_write() - Write image to NAND for fastboot |
| 150 | * |
| 151 | * @cmd: Named device to write image to |
| 152 | * @download_buffer: Pointer to image data |
| 153 | * @download_bytes: Size of image data |
| 154 | * @response: Pointer to fastboot response buffer |
| 155 | */ |
| 156 | void fastboot_nand_flash_write(const char *cmd, void *download_buffer, |
| 157 | unsigned int download_bytes, char *response) |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 158 | { |
| 159 | struct part_info *part; |
Scott Wood | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 160 | struct mtd_info *mtd = NULL; |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 161 | int ret; |
| 162 | |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 163 | ret = fb_nand_lookup(cmd, &mtd, &part, response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 164 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 165 | pr_err("invalid NAND device"); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 166 | fastboot_fail("invalid NAND device", response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 167 | return; |
| 168 | } |
| 169 | |
Maxime Ripard | 1812354 | 2015-10-15 14:34:18 +0200 | [diff] [blame] | 170 | ret = board_fastboot_write_partition_setup(part->name); |
| 171 | if (ret) |
| 172 | return; |
| 173 | |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 174 | if (is_sparse_image(download_buffer)) { |
| 175 | struct fb_nand_sparse sparse_priv; |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 176 | struct sparse_storage sparse; |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 177 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 178 | sparse_priv.mtd = mtd; |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 179 | sparse_priv.part = part; |
| 180 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 181 | sparse.blksz = mtd->writesize; |
| 182 | sparse.start = part->offset / sparse.blksz; |
| 183 | sparse.size = part->size / sparse.blksz; |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 184 | sparse.write = fb_nand_sparse_write; |
Steve Rae | 8a99be8 | 2016-06-07 11:19:38 -0700 | [diff] [blame] | 185 | sparse.reserve = fb_nand_sparse_reserve; |
Jassi Brar | 427e669 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 186 | sparse.mssg = fastboot_fail; |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 187 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 188 | printf("Flashing sparse image at offset " LBAFU "\n", |
| 189 | sparse.start); |
| 190 | |
| 191 | sparse.priv = &sparse_priv; |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 192 | ret = write_sparse_image(&sparse, cmd, download_buffer, |
| 193 | response); |
Jassi Brar | 427e669 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 194 | if (!ret) |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 195 | fastboot_okay(NULL, response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 196 | } else { |
| 197 | printf("Flashing raw image at offset 0x%llx\n", |
| 198 | part->offset); |
| 199 | |
Scott Wood | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 200 | ret = _fb_nand_write(mtd, part, download_buffer, part->offset, |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 201 | download_bytes, NULL); |
| 202 | |
| 203 | printf("........ wrote %u bytes to '%s'\n", |
| 204 | download_bytes, part->name); |
| 205 | } |
| 206 | |
| 207 | if (ret) { |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 208 | fastboot_fail("error writing the image", response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 209 | return; |
| 210 | } |
| 211 | |
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 | } |
| 214 | |
Alex Kiernan | 32c2194 | 2018-05-29 15:30:48 +0000 | [diff] [blame^] | 215 | /** |
| 216 | * fastboot_nand_flash_erase() - Erase NAND for fastboot |
| 217 | * |
| 218 | * @cmd: Named device to erase |
| 219 | * @response: Pointer to fastboot response buffer |
| 220 | */ |
| 221 | void fastboot_nand_erase(const char *cmd, char *response) |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 222 | { |
| 223 | struct part_info *part; |
Scott Wood | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 224 | struct mtd_info *mtd = NULL; |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 225 | int ret; |
| 226 | |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 227 | ret = fb_nand_lookup(cmd, &mtd, &part, response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 228 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 229 | pr_err("invalid NAND device"); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 230 | fastboot_fail("invalid NAND device", response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 231 | return; |
| 232 | } |
| 233 | |
Maxime Ripard | 1812354 | 2015-10-15 14:34:18 +0200 | [diff] [blame] | 234 | ret = board_fastboot_erase_partition_setup(part->name); |
| 235 | if (ret) |
| 236 | return; |
| 237 | |
Scott Wood | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 238 | ret = _fb_nand_erase(mtd, part); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 239 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 240 | pr_err("failed erasing from device %s", mtd->name); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 241 | fastboot_fail("failed erasing from device", response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 242 | return; |
| 243 | } |
| 244 | |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 245 | fastboot_okay(NULL, response); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 246 | } |