Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Parse RedBoot-style Flash Image System (FIS) tables and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * produce a Linux partition array to match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright © 2001      Red Hat UK Limited
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Copyright © 2001-2010 David Woodhouse <dwmw2@infradead.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/mtd/mtd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/mtd/partitions.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) struct fis_image_desc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)     unsigned char name[16];      // Null terminated name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)     uint32_t	  flash_base;    // Address within FLASH of image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)     uint32_t	  mem_base;      // Address in memory where it executes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)     uint32_t	  size;          // Length of image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)     uint32_t	  entry_point;   // Execution entry point
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)     uint32_t	  data_length;   // Length of actual data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)     unsigned char _pad[256-(16+7*sizeof(uint32_t))];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)     uint32_t	  desc_cksum;    // Checksum over image descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)     uint32_t	  file_cksum;    // Checksum over image data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) struct fis_list {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	struct fis_image_desc *img;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct fis_list *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static int directory = CONFIG_MTD_REDBOOT_DIRECTORY_BLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) module_param(directory, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) static inline int redboot_checksum(struct fis_image_desc *img)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	/* RedBoot doesn't actually write the desc_cksum field yet AFAICT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static void parse_redboot_of(struct mtd_info *master)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct device_node *np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	struct device_node *npart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	u32 dirblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	np = mtd_get_of_node(master);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	if (!np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	npart = of_get_child_by_name(np, "partitions");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	if (!npart)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	ret = of_property_read_u32(npart, "fis-index-block", &dirblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	 * Assign the block found in the device tree to the local
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	 * directory block pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	directory = dirblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static int parse_redboot_partitions(struct mtd_info *master,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 				    const struct mtd_partition **pparts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 				    struct mtd_part_parser_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	int nrparts = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct fis_image_desc *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	struct mtd_partition *parts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	struct fis_list *fl = NULL, *tmp_fl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	int ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	size_t retlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	char *names;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	char *nullname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	int namelen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	int nulllen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	int numslots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	static char nullstring[] = "unallocated";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	parse_redboot_of(master);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	if ( directory < 0 ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		offset = master->size + directory * master->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		while (mtd_block_isbad(master, offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			if (!offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			nogood:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 				printk(KERN_NOTICE "Failed to find a non-bad block to check for RedBoot partition table\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 				return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			offset -= master->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		offset = directory * master->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		while (mtd_block_isbad(master, offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			offset += master->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			if (offset == master->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 				goto nogood;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	buf = vmalloc(master->erasesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	printk(KERN_NOTICE "Searching for RedBoot partition table in %s at offset 0x%lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	       master->name, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	ret = mtd_read(master, offset, master->erasesize, &retlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		       (void *)buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	if (retlen != master->erasesize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	numslots = (master->erasesize / sizeof(struct fis_image_desc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	for (i = 0; i < numslots; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		if (!memcmp(buf[i].name, "FIS directory", 14)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			/* This is apparently the FIS directory entry for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			 * FIS directory itself.  The FIS directory size is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			 * one erase block; if the buf[i].size field is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			 * swab32(erasesize) then we know we are looking at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			 * a byte swapped FIS directory - swap all the entries!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			 * (NOTE: this is 'size' not 'data_length'; size is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			 * the full size of the entry.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			/* RedBoot can combine the FIS directory and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			   config partitions into a single eraseblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			   we assume wrong-endian if either the swapped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			   'size' matches the eraseblock size precisely,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			   or if the swapped size actually fits in an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			   eraseblock while the unswapped size doesn't. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			if (swab32(buf[i].size) == master->erasesize ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			    (buf[i].size > master->erasesize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			     && swab32(buf[i].size) < master->erasesize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 				int j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 				/* Update numslots based on actual FIS directory size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 				numslots = swab32(buf[i].size) / sizeof (struct fis_image_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 				for (j = 0; j < numslots; ++j) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 					/* A single 0xff denotes a deleted entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 					 * Two of them in a row is the end of the table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 					 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 					if (buf[j].name[0] == 0xff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 				  		if (buf[j].name[1] == 0xff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 							break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 						} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 							continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 						}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 					}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 					/* The unsigned long fields were written with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 					 * wrong byte sex, name and pad have no byte sex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 					 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 					swab32s(&buf[j].flash_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 					swab32s(&buf[j].mem_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 					swab32s(&buf[j].size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 					swab32s(&buf[j].entry_point);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 					swab32s(&buf[j].data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 					swab32s(&buf[j].desc_cksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 					swab32s(&buf[j].file_cksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 			} else if (buf[i].size < master->erasesize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 				/* Update numslots based on actual FIS directory size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 				numslots = buf[i].size / sizeof(struct fis_image_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	if (i == numslots) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		/* Didn't find it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		printk(KERN_NOTICE "No RedBoot partition table detected in %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		       master->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	for (i = 0; i < numslots; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		struct fis_list *new_fl, **prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		if (buf[i].name[0] == 0xff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 			if (buf[i].name[1] == 0xff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		if (!redboot_checksum(&buf[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		new_fl = kmalloc(sizeof(struct fis_list), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		namelen += strlen(buf[i].name)+1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		if (!new_fl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 			ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		new_fl->img = &buf[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		if (data && data->origin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			buf[i].flash_base -= data->origin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			buf[i].flash_base &= master->size-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		/* I'm sure the JFFS2 code has done me permanent damage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		 * I now think the following is _normal_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		prev = &fl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		while(*prev && (*prev)->img->flash_base < new_fl->img->flash_base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 			prev = &(*prev)->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		new_fl->next = *prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		*prev = new_fl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		nrparts++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	if (fl->img->flash_base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		nrparts++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		nulllen = sizeof(nullstring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	for (tmp_fl = fl; tmp_fl->next; tmp_fl = tmp_fl->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		if (tmp_fl->img->flash_base + tmp_fl->img->size + master->erasesize <= tmp_fl->next->img->flash_base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			nrparts++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			nulllen = sizeof(nullstring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	parts = kzalloc(sizeof(*parts)*nrparts + nulllen + namelen, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	if (!parts) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	nullname = (char *)&parts[nrparts];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	if (nulllen > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		strcpy(nullname, nullstring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	names = nullname + nulllen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	i=0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	if (fl->img->flash_base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	       parts[0].name = nullname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	       parts[0].size = fl->img->flash_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	       parts[0].offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	for ( ; i<nrparts; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		parts[i].size = fl->img->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		parts[i].offset = fl->img->flash_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		parts[i].name = names;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		strcpy(names, fl->img->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) #ifdef CONFIG_MTD_REDBOOT_PARTS_READONLY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		if (!memcmp(names, "RedBoot", 8) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 				!memcmp(names, "RedBoot config", 15) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 				!memcmp(names, "FIS directory", 14)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			parts[i].mask_flags = MTD_WRITEABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		names += strlen(names)+1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		if(fl->next && fl->img->flash_base + fl->img->size + master->erasesize <= fl->next->img->flash_base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			parts[i].offset = parts[i-1].size + parts[i-1].offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 			parts[i].size = fl->next->img->flash_base - parts[i].offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 			parts[i].name = nullname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		tmp_fl = fl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		fl = fl->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		kfree(tmp_fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	ret = nrparts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	*pparts = parts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	while (fl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		struct fis_list *old = fl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		fl = fl->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		kfree(old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	vfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) static const struct of_device_id mtd_parser_redboot_of_match_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	{ .compatible = "redboot-fis" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) MODULE_DEVICE_TABLE(of, mtd_parser_redboot_of_match_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) static struct mtd_part_parser redboot_parser = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	.parse_fn = parse_redboot_partitions,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	.name = "RedBoot",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	.of_match_table = mtd_parser_redboot_of_match_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) module_mtd_part_parser(redboot_parser);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) /* mtd parsers will request the module by parser name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) MODULE_ALIAS("RedBoot");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) MODULE_DESCRIPTION("Parsing code for RedBoot Flash Image System (FIS) tables");