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