treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / mtd / parsers / ar7part.c
blob8cd683711ac609fcc0ac1bd5588fd8ce5b17415a
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright © 2007 Eugene Konev <ejka@openwrt.org>
5 * TI AR7 flash partition table.
6 * Based on ar7 map by Felix Fietkau <nbd@openwrt.org>
7 */
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
12 #include <linux/mtd/mtd.h>
13 #include <linux/mtd/partitions.h>
14 #include <linux/memblock.h>
15 #include <linux/module.h>
17 #include <uapi/linux/magic.h>
19 #define AR7_PARTS 4
20 #define ROOT_OFFSET 0xe0000
22 #define LOADER_MAGIC1 le32_to_cpu(0xfeedfa42)
23 #define LOADER_MAGIC2 le32_to_cpu(0xfeed1281)
25 struct ar7_bin_rec {
26 unsigned int checksum;
27 unsigned int length;
28 unsigned int address;
31 static int create_mtd_partitions(struct mtd_info *master,
32 const struct mtd_partition **pparts,
33 struct mtd_part_parser_data *data)
35 struct ar7_bin_rec header;
36 unsigned int offset;
37 size_t len;
38 unsigned int pre_size = master->erasesize, post_size = 0;
39 unsigned int root_offset = ROOT_OFFSET;
41 int retries = 10;
42 struct mtd_partition *ar7_parts;
44 ar7_parts = kcalloc(AR7_PARTS, sizeof(*ar7_parts), GFP_KERNEL);
45 if (!ar7_parts)
46 return -ENOMEM;
47 ar7_parts[0].name = "loader";
48 ar7_parts[0].offset = 0;
49 ar7_parts[0].size = master->erasesize;
50 ar7_parts[0].mask_flags = MTD_WRITEABLE;
52 ar7_parts[1].name = "config";
53 ar7_parts[1].offset = 0;
54 ar7_parts[1].size = master->erasesize;
55 ar7_parts[1].mask_flags = 0;
57 do { /* Try 10 blocks starting from master->erasesize */
58 offset = pre_size;
59 mtd_read(master, offset, sizeof(header), &len,
60 (uint8_t *)&header);
61 if (!strncmp((char *)&header, "TIENV0.8", 8))
62 ar7_parts[1].offset = pre_size;
63 if (header.checksum == LOADER_MAGIC1)
64 break;
65 if (header.checksum == LOADER_MAGIC2)
66 break;
67 pre_size += master->erasesize;
68 } while (retries--);
70 pre_size = offset;
72 if (!ar7_parts[1].offset) {
73 ar7_parts[1].offset = master->size - master->erasesize;
74 post_size = master->erasesize;
77 switch (header.checksum) {
78 case LOADER_MAGIC1:
79 while (header.length) {
80 offset += sizeof(header) + header.length;
81 mtd_read(master, offset, sizeof(header), &len,
82 (uint8_t *)&header);
84 root_offset = offset + sizeof(header) + 4;
85 break;
86 case LOADER_MAGIC2:
87 while (header.length) {
88 offset += sizeof(header) + header.length;
89 mtd_read(master, offset, sizeof(header), &len,
90 (uint8_t *)&header);
92 root_offset = offset + sizeof(header) + 4 + 0xff;
93 root_offset &= ~(uint32_t)0xff;
94 break;
95 default:
96 printk(KERN_WARNING "Unknown magic: %08x\n", header.checksum);
97 break;
100 mtd_read(master, root_offset, sizeof(header), &len, (u8 *)&header);
101 if (header.checksum != SQUASHFS_MAGIC) {
102 root_offset += master->erasesize - 1;
103 root_offset &= ~(master->erasesize - 1);
106 ar7_parts[2].name = "linux";
107 ar7_parts[2].offset = pre_size;
108 ar7_parts[2].size = master->size - pre_size - post_size;
109 ar7_parts[2].mask_flags = 0;
111 ar7_parts[3].name = "rootfs";
112 ar7_parts[3].offset = root_offset;
113 ar7_parts[3].size = master->size - root_offset - post_size;
114 ar7_parts[3].mask_flags = 0;
116 *pparts = ar7_parts;
117 return AR7_PARTS;
120 static struct mtd_part_parser ar7_parser = {
121 .parse_fn = create_mtd_partitions,
122 .name = "ar7part",
124 module_mtd_part_parser(ar7_parser);
126 MODULE_LICENSE("GPL");
127 MODULE_AUTHOR( "Felix Fietkau <nbd@openwrt.org>, "
128 "Eugene Konev <ejka@openwrt.org>");
129 MODULE_DESCRIPTION("MTD partitioning for TI AR7");