Linux Audio

Check our new training course

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