201cdd3f87fa6615da536a9b1766a6e0f4e3ece5
[openwrt/openwrt.git] / target / linux / lantiq / patches-5.4 / 0101-find_active_root.patch
1 --- a/drivers/mtd/parsers/ofpart_core.c
2 +++ b/drivers/mtd/parsers/ofpart_core.c
3 @@ -33,6 +33,38 @@ static bool node_has_compatible(struct d
4 return of_get_property(pp, "compatible", NULL);
5 }
6
7 +static uint8_t * brnboot_get_selected_root_part(struct mtd_info *master,
8 + loff_t offset)
9 +{
10 + static uint8_t root_id;
11 + int err, len;
12 +
13 + err = mtd_read(master, offset, 0x01, &len, &root_id);
14 +
15 + if (mtd_is_bitflip(err) || !err)
16 + return &root_id;
17 +
18 + return NULL;
19 +}
20 +
21 +static void brnboot_set_active_root_part(struct mtd_partition *pparts,
22 + struct device_node **part_nodes,
23 + int nr_parts,
24 + uint8_t *root_id)
25 +{
26 + int i;
27 +
28 + for (i = 0; i < nr_parts; i++) {
29 + int part_root_id;
30 +
31 + if (!of_property_read_u32(part_nodes[i], "brnboot,root-id", &part_root_id)
32 + && part_root_id == *root_id) {
33 + pparts[i].name = "firmware";
34 + break;
35 + }
36 + }
37 +}
38 +
39 static int parse_fixed_partitions(struct mtd_info *master,
40 const struct mtd_partition **pparts,
41 struct mtd_part_parser_data *data)
42 @@ -46,6 +78,8 @@ static int parse_fixed_partitions(struct
43 struct device_node *pp;
44 int nr_parts, i, ret = 0;
45 bool dedicated = true;
46 + uint8_t *proot_id = NULL;
47 + struct device_node **part_nodes;
48
49 /* Pull of_node from the master device node */
50 mtd_node = mtd_get_of_node(master);
51 @@ -88,7 +122,9 @@ static int parse_fixed_partitions(struct
52 return 0;
53
54 parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
55 - if (!parts)
56 + part_nodes = kcalloc(nr_parts, sizeof(*part_nodes), GFP_KERNEL);
57 +
58 + if (!parts || !part_nodes)
59 return -ENOMEM;
60
61 i = 0;
62 @@ -137,6 +173,11 @@ static int parse_fixed_partitions(struct
63 if (of_get_property(pp, "lock", &len))
64 parts[i].mask_flags |= MTD_POWERUP_LOCK;
65
66 + if (!proot_id && of_device_is_compatible(pp, "brnboot,root-selector"))
67 + proot_id = brnboot_get_selected_root_part(master, parts[i].offset);
68 +
69 + part_nodes[i] = pp;
70 +
71 i++;
72 }
73
74 @@ -146,6 +187,11 @@ static int parse_fixed_partitions(struct
75 if (quirks && quirks->post_parse)
76 quirks->post_parse(master, parts, nr_parts);
77
78 + if (proot_id)
79 + brnboot_set_active_root_part(parts, part_nodes, nr_parts, proot_id);
80 +
81 + kfree(part_nodes);
82 +
83 *pparts = parts;
84 return nr_parts;
85
86 @@ -156,6 +202,7 @@ ofpart_fail:
87 ofpart_none:
88 of_node_put(pp);
89 kfree(parts);
90 + kfree(part_nodes);
91 return ret;
92 }
93