rpcd: iwinfo plugin fixes
[openwrt.git] / target / linux / lantiq / patches-4.4 / 0101-find_active_root.patch
1 --- a/drivers/mtd/ofpart.c
2 +++ b/drivers/mtd/ofpart.c
3 @@ -25,6 +25,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_ofpart_partitions(struct mtd_info *master,
40                                    struct mtd_partition **pparts,
41                                    struct mtd_part_parser_data *data)
42 @@ -35,7 +67,8 @@ static int parse_ofpart_partitions(struc
43         struct device_node *pp;
44         int nr_parts, i, ret = 0;
45         bool dedicated = true;
46 -
47 +       uint8_t *proot_id = NULL;
48 +       struct device_node **part_nodes;
49  
50         if (!data)
51                 return 0;
52 @@ -73,7 +106,9 @@ static int parse_ofpart_partitions(struc
53                 return 0;
54  
55         *pparts = kzalloc(nr_parts * sizeof(**pparts), GFP_KERNEL);
56 -       if (!*pparts)
57 +       part_nodes = kzalloc(nr_parts * sizeof(*part_nodes), GFP_KERNEL);
58 +
59 +       if (!*pparts || !part_nodes)
60                 return -ENOMEM;
61  
62         i = 0;
63 @@ -121,12 +156,22 @@ static int parse_ofpart_partitions(struc
64                 if (of_get_property(pp, "lock", &len))
65                         (*pparts)[i].mask_flags |= MTD_POWERUP_LOCK;
66  
67 +               if (!proot_id && of_device_is_compatible(pp, "brnboot,root-selector"))
68 +                       proot_id = brnboot_get_selected_root_part(master, (*pparts)[i].offset);
69 +
70 +               part_nodes[i] = pp;
71 +
72                 i++;
73         }
74  
75         if (!nr_parts)
76                 goto ofpart_none;
77  
78 +       if (proot_id)
79 +               brnboot_set_active_root_part(*pparts, part_nodes, nr_parts, proot_id);
80 +
81 +       kfree(part_nodes);
82 +
83         return nr_parts;
84  
85  ofpart_fail:
86 @@ -136,6 +181,7 @@ ofpart_fail:
87  ofpart_none:
88         of_node_put(pp);
89         kfree(*pparts);
90 +       kfree(part_nodes);
91         *pparts = NULL;
92         return ret;
93  }