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