root/drivers/mtd/parsers/parser_imagetag.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. bcm963xx_read_imagetag
  2. bcm963xx_parse_imagetag_partitions

   1 // SPDX-License-Identifier: GPL-2.0-or-later
   2 /*
   3  * BCM63XX CFE image tag parser
   4  *
   5  * Copyright © 2006-2008  Florian Fainelli <florian@openwrt.org>
   6  *                        Mike Albon <malbon@openwrt.org>
   7  * Copyright © 2009-2010  Daniel Dickinson <openwrt@cshore.neomailbox.net>
   8  * Copyright © 2011-2013  Jonas Gorski <jonas.gorski@gmail.com>
   9  */
  10 
  11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12 
  13 #include <linux/bcm963xx_tag.h>
  14 #include <linux/crc32.h>
  15 #include <linux/module.h>
  16 #include <linux/kernel.h>
  17 #include <linux/sizes.h>
  18 #include <linux/slab.h>
  19 #include <linux/vmalloc.h>
  20 #include <linux/mtd/mtd.h>
  21 #include <linux/mtd/partitions.h>
  22 #include <linux/of.h>
  23 
  24 /* Ensure strings read from flash structs are null terminated */
  25 #define STR_NULL_TERMINATE(x) \
  26         do { char *_str = (x); _str[sizeof(x) - 1] = 0; } while (0)
  27 
  28 static int bcm963xx_read_imagetag(struct mtd_info *master, const char *name,
  29         loff_t tag_offset, struct bcm_tag *buf)
  30 {
  31         int ret;
  32         size_t retlen;
  33         u32 computed_crc;
  34 
  35         ret = mtd_read(master, tag_offset, sizeof(*buf), &retlen, (void *)buf);
  36         if (ret)
  37                 return ret;
  38 
  39         if (retlen != sizeof(*buf))
  40                 return -EIO;
  41 
  42         computed_crc = crc32_le(IMAGETAG_CRC_START, (u8 *)buf,
  43                                 offsetof(struct bcm_tag, header_crc));
  44         if (computed_crc == buf->header_crc) {
  45                 STR_NULL_TERMINATE(buf->board_id);
  46                 STR_NULL_TERMINATE(buf->tag_version);
  47 
  48                 pr_info("%s: CFE image tag found at 0x%llx with version %s, board type %s\n",
  49                         name, tag_offset, buf->tag_version, buf->board_id);
  50 
  51                 return 0;
  52         }
  53 
  54         pr_warn("%s: CFE image tag at 0x%llx CRC invalid (expected %08x, actual %08x)\n",
  55                 name, tag_offset, buf->header_crc, computed_crc);
  56         return -EINVAL;
  57 }
  58 
  59 static int bcm963xx_parse_imagetag_partitions(struct mtd_info *master,
  60                                         const struct mtd_partition **pparts,
  61                                         struct mtd_part_parser_data *data)
  62 {
  63         /* CFE, NVRAM and global Linux are always present */
  64         int nrparts = 0, curpart = 0;
  65         struct bcm_tag *buf = NULL;
  66         struct mtd_partition *parts;
  67         int ret;
  68         unsigned int rootfsaddr, kerneladdr, spareaddr, offset;
  69         unsigned int rootfslen, kernellen, sparelen, totallen;
  70         int i;
  71         bool rootfs_first = false;
  72 
  73         buf = vmalloc(sizeof(struct bcm_tag));
  74         if (!buf)
  75                 return -ENOMEM;
  76 
  77         /* Get the tag */
  78         ret = bcm963xx_read_imagetag(master, "rootfs", 0, buf);
  79         if (!ret) {
  80                 STR_NULL_TERMINATE(buf->flash_image_start);
  81                 if (kstrtouint(buf->flash_image_start, 10, &rootfsaddr) ||
  82                                 rootfsaddr < BCM963XX_EXTENDED_SIZE) {
  83                         pr_err("invalid rootfs address: %*ph\n",
  84                                 (int)sizeof(buf->flash_image_start),
  85                                 buf->flash_image_start);
  86                         goto out;
  87                 }
  88 
  89                 STR_NULL_TERMINATE(buf->kernel_address);
  90                 if (kstrtouint(buf->kernel_address, 10, &kerneladdr) ||
  91                                 kerneladdr < BCM963XX_EXTENDED_SIZE) {
  92                         pr_err("invalid kernel address: %*ph\n",
  93                                 (int)sizeof(buf->kernel_address),
  94                                 buf->kernel_address);
  95                         goto out;
  96                 }
  97 
  98                 STR_NULL_TERMINATE(buf->kernel_length);
  99                 if (kstrtouint(buf->kernel_length, 10, &kernellen)) {
 100                         pr_err("invalid kernel length: %*ph\n",
 101                                 (int)sizeof(buf->kernel_length),
 102                                 buf->kernel_length);
 103                         goto out;
 104                 }
 105 
 106                 STR_NULL_TERMINATE(buf->total_length);
 107                 if (kstrtouint(buf->total_length, 10, &totallen)) {
 108                         pr_err("invalid total length: %*ph\n",
 109                                 (int)sizeof(buf->total_length),
 110                                 buf->total_length);
 111                         goto out;
 112                 }
 113 
 114                 /*
 115                  * Addresses are flash absolute, so convert to partition
 116                  * relative addresses. Assume either kernel or rootfs will
 117                  * directly follow the image tag.
 118                  */
 119                 if (rootfsaddr < kerneladdr)
 120                         offset = rootfsaddr - sizeof(struct bcm_tag);
 121                 else
 122                         offset = kerneladdr - sizeof(struct bcm_tag);
 123 
 124                 kerneladdr = kerneladdr - offset;
 125                 rootfsaddr = rootfsaddr - offset;
 126                 spareaddr = roundup(totallen, master->erasesize);
 127 
 128                 if (rootfsaddr < kerneladdr) {
 129                         /* default Broadcom layout */
 130                         rootfslen = kerneladdr - rootfsaddr;
 131                         rootfs_first = true;
 132                 } else {
 133                         /* OpenWrt layout */
 134                         rootfsaddr = kerneladdr + kernellen;
 135                         rootfslen = spareaddr - rootfsaddr;
 136                 }
 137         } else {
 138                 goto out;
 139         }
 140         sparelen = master->size - spareaddr;
 141 
 142         /* Determine number of partitions */
 143         if (rootfslen > 0)
 144                 nrparts++;
 145 
 146         if (kernellen > 0)
 147                 nrparts++;
 148 
 149         parts = kzalloc(sizeof(*parts) * nrparts + 10 * nrparts, GFP_KERNEL);
 150         if (!parts) {
 151                 ret = -ENOMEM;
 152                 goto out;
 153         }
 154 
 155         /* Start building partition list */
 156         if (kernellen > 0) {
 157                 int kernelpart = curpart;
 158 
 159                 if (rootfslen > 0 && rootfs_first)
 160                         kernelpart++;
 161                 parts[kernelpart].name = "kernel";
 162                 parts[kernelpart].offset = kerneladdr;
 163                 parts[kernelpart].size = kernellen;
 164                 curpart++;
 165         }
 166 
 167         if (rootfslen > 0) {
 168                 int rootfspart = curpart;
 169 
 170                 if (kernellen > 0 && rootfs_first)
 171                         rootfspart--;
 172                 parts[rootfspart].name = "rootfs";
 173                 parts[rootfspart].offset = rootfsaddr;
 174                 parts[rootfspart].size = rootfslen;
 175                 if (sparelen > 0  && !rootfs_first)
 176                         parts[rootfspart].size += sparelen;
 177                 curpart++;
 178         }
 179 
 180         for (i = 0; i < nrparts; i++)
 181                 pr_info("Partition %d is %s offset %llx and length %llx\n", i,
 182                         parts[i].name, parts[i].offset, parts[i].size);
 183 
 184         pr_info("Spare partition is offset %x and length %x\n", spareaddr,
 185                 sparelen);
 186 
 187         *pparts = parts;
 188         ret = 0;
 189 
 190 out:
 191         vfree(buf);
 192 
 193         if (ret)
 194                 return ret;
 195 
 196         return nrparts;
 197 }
 198 
 199 static const struct of_device_id parse_bcm963xx_imagetag_match_table[] = {
 200         { .compatible = "brcm,bcm963xx-imagetag" },
 201         {},
 202 };
 203 MODULE_DEVICE_TABLE(of, parse_bcm963xx_imagetag_match_table);
 204 
 205 static struct mtd_part_parser bcm963xx_imagetag_parser = {
 206         .parse_fn = bcm963xx_parse_imagetag_partitions,
 207         .name = "bcm963xx-imagetag",
 208         .of_match_table = parse_bcm963xx_imagetag_match_table,
 209 };
 210 module_mtd_part_parser(bcm963xx_imagetag_parser);
 211 
 212 MODULE_LICENSE("GPL");
 213 MODULE_AUTHOR("Daniel Dickinson <openwrt@cshore.neomailbox.net>");
 214 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
 215 MODULE_AUTHOR("Mike Albon <malbon@openwrt.org>");
 216 MODULE_AUTHOR("Jonas Gorski <jonas.gorski@gmail.com>");
 217 MODULE_DESCRIPTION("MTD parser for BCM963XX CFE Image Tag partitions");

/* [<][>][^][v][top][bottom][index][help] */