a204fe6625dcb33a6b36707ddf28de69fc5f49f6
[oweals/openwrt.git] /
1 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
2 Subject: [PATCH 1/2] mtd: bcm47xxpart: move TRX parsing code to separated
3  function
4 MIME-Version: 1.0
5 Content-Type: text/plain; charset=UTF-8
6 Content-Transfer-Encoding: 8bit
7
8 This change simplifies main parsing loop logic a bit. In future it may
9 be useful for moving TRX support to separated module / parser (if we
10 implement support for them at some point).
11 Finally parsing TRX at the end puts us in a better position as we have
12 better flash layout knowledge. It may be useful e.g. if it appears there
13 is more than 1 TRX partition.
14
15 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
16 ---
17  drivers/mtd/bcm47xxpart.c | 121 ++++++++++++++++++++++++++++------------------
18  1 file changed, 74 insertions(+), 47 deletions(-)
19
20 --- a/drivers/mtd/bcm47xxpart.c
21 +++ b/drivers/mtd/bcm47xxpart.c
22 @@ -83,6 +83,67 @@ out_default:
23         return "rootfs";
24  }
25  
26 +static int bcm47xxpart_parse_trx(struct mtd_info *master,
27 +                                struct mtd_partition *trx,
28 +                                struct mtd_partition *parts,
29 +                                size_t parts_len)
30 +{
31 +       struct trx_header header;
32 +       size_t bytes_read;
33 +       int curr_part = 0;
34 +       int i, err;
35 +
36 +       if (parts_len < 3) {
37 +               pr_warn("No enough space to add TRX partitions!\n");
38 +               return -ENOMEM;
39 +       }
40 +
41 +       err = mtd_read(master, trx->offset, sizeof(header), &bytes_read,
42 +                      (uint8_t *)&header);
43 +       if (err && !mtd_is_bitflip(err)) {
44 +               pr_err("mtd_read error while reading TRX header: %d\n", err);
45 +               return err;
46 +       }
47 +
48 +       i = 0;
49 +
50 +       /* We have LZMA loader if offset[2] points to sth */
51 +       if (header.offset[2]) {
52 +               bcm47xxpart_add_part(&parts[curr_part++], "loader",
53 +                                    trx->offset + header.offset[i], 0);
54 +               i++;
55 +       }
56 +
57 +       if (header.offset[i]) {
58 +               bcm47xxpart_add_part(&parts[curr_part++], "linux",
59 +                                    trx->offset + header.offset[i], 0);
60 +               i++;
61 +       }
62 +
63 +       if (header.offset[i]) {
64 +               size_t offset = trx->offset + header.offset[i];
65 +               const char *name = bcm47xxpart_trx_data_part_name(master,
66 +                                                                 offset);
67 +
68 +               bcm47xxpart_add_part(&parts[curr_part++], name, offset, 0);
69 +               i++;
70 +       }
71 +
72 +       /*
73 +        * Assume that every partition ends at the beginning of the one it is
74 +        * followed by.
75 +        */
76 +       for (i = 0; i < curr_part; i++) {
77 +               u64 next_part_offset = (i < curr_part - 1) ?
78 +                                       parts[i + 1].offset :
79 +                                       trx->offset + trx->size;
80 +
81 +               parts[i].size = next_part_offset - parts[i].offset;
82 +       }
83 +
84 +       return curr_part;
85 +}
86 +
87  static int bcm47xxpart_parse(struct mtd_info *master,
88                              struct mtd_partition **pparts,
89                              struct mtd_part_parser_data *data)
90 @@ -93,9 +154,7 @@ static int bcm47xxpart_parse(struct mtd_
91         size_t bytes_read;
92         uint32_t offset;
93         uint32_t blocksize = master->erasesize;
94 -       struct trx_header *trx;
95         int trx_part = -1;
96 -       int last_trx_part = -1;
97         int possible_nvram_sizes[] = { 0x8000, 0xF000, 0x10000, };
98         int err;
99  
100 @@ -182,54 +241,14 @@ static int bcm47xxpart_parse(struct mtd_
101  
102                 /* TRX */
103                 if (buf[0x000 / 4] == TRX_MAGIC) {
104 -                       if (BCM47XXPART_MAX_PARTS - curr_part < 4) {
105 -                               pr_warn("Not enough partitions left to register trx, scanning stopped!\n");
106 -                               break;
107 -                       }
108 -
109 -                       trx = (struct trx_header *)buf;
110 +                       struct trx_header *trx;
111  
112                         trx_part = curr_part;
113                         bcm47xxpart_add_part(&parts[curr_part++], "firmware",
114                                              offset, 0);
115  
116 -                       i = 0;
117 -                       /* We have LZMA loader if offset[2] points to sth */
118 -                       if (trx->offset[2]) {
119 -                               bcm47xxpart_add_part(&parts[curr_part++],
120 -                                                    "loader",
121 -                                                    offset + trx->offset[i],
122 -                                                    0);
123 -                               i++;
124 -                       }
125 -
126 -                       if (trx->offset[i]) {
127 -                               bcm47xxpart_add_part(&parts[curr_part++],
128 -                                                    "linux",
129 -                                                    offset + trx->offset[i],
130 -                                                    0);
131 -                               i++;
132 -                       }
133 -
134 -                       /*
135 -                        * Pure rootfs size is known and can be calculated as:
136 -                        * trx->length - trx->offset[i]. We don't fill it as
137 -                        * we want to have jffs2 (overlay) in the same mtd.
138 -                        */
139 -                       if (trx->offset[i]) {
140 -                               const char *name;
141 -
142 -                               name = bcm47xxpart_trx_data_part_name(master, offset + trx->offset[i]);
143 -                               bcm47xxpart_add_part(&parts[curr_part++],
144 -                                                    name,
145 -                                                    offset + trx->offset[i],
146 -                                                    0);
147 -                               i++;
148 -                       }
149 -
150 -                       last_trx_part = curr_part - 1;
151 -
152                         /* Jump to the end of TRX */
153 +                       trx = (struct trx_header *)buf;
154                         offset = roundup(offset + trx->length, blocksize);
155                         /* Next loop iteration will increase the offset */
156                         offset -= blocksize;
157 @@ -307,9 +326,17 @@ static int bcm47xxpart_parse(struct mtd_
158                                        parts[i + 1].offset : master->size;
159  
160                 parts[i].size = next_part_offset - parts[i].offset;
161 -               if (i == last_trx_part && trx_part >= 0)
162 -                       parts[trx_part].size = next_part_offset -
163 -                                              parts[trx_part].offset;
164 +       }
165 +
166 +       /* If there was TRX parse it now */
167 +       if (trx_part >= 0) {
168 +               int num_parts;
169 +
170 +               num_parts = bcm47xxpart_parse_trx(master, &parts[trx_part],
171 +                                                 parts + curr_part,
172 +                                                 BCM47XXPART_MAX_PARTS - curr_part);
173 +               if (num_parts > 0)
174 +                       curr_part += num_parts;
175         }
176  
177         *pparts = parts;