1 #include <string>
2 #include <utility>
3 
4 #include <gtest/gtest.h>
5 
6 using PartClear = std::pair<std::string, bool>;
7 namespace utils
8 {
9 extern std::vector<PartClear> getPartsToClear(const std::string& info);
10 }
11 
12 TEST(TestItemUpdaterStatic, getPartsToClearOK)
13 {
14     constexpr auto info =
15         "Flash info:\n"
16         "-----------\n"
17         "Name          = /dev/mtd6\n"
18         "Total size    = 64MB     Flags E:ECC, P:PRESERVED, R:READONLY, "
19         "B:BACKUP\n"
20         "Erase granule = 64KB           F:REPROVISION, V:VOLATILE, C:CLEARECC\n"
21         "\n"
22         "TOC@0x00000000 Partitions:\n"
23         "-----------\n"
24         "ID=00        part 0x00000000..0x00002000 (actual=0x00002000) "
25         "[----------]\n"
26         "ID=01        HBEL 0x00008000..0x0002c000 (actual=0x00024000) "
27         "[E-----F-C-]\n"
28         "ID=02       GUARD 0x0002c000..0x00031000 (actual=0x00005000) "
29         "[E--P--F-C-]\n"
30         "ID=03       NVRAM 0x00031000..0x000c1000 (actual=0x00090000) "
31         "[---P--F---]\n"
32         "ID=04     SECBOOT 0x000c1000..0x000e5000 (actual=0x00024000) "
33         "[E--P------]\n"
34         "ID=05       DJVPD 0x000e5000..0x0012d000 (actual=0x00048000) "
35         "[E--P--F-C-]\n"
36         "ID=06        MVPD 0x0012d000..0x001bd000 (actual=0x00090000) "
37         "[E--P--F-C-]\n"
38         "ID=07        CVPD 0x001bd000..0x00205000 (actual=0x00048000) "
39         "[E--P--F-C-]\n"
40         "ID=08         HBB 0x00205000..0x00305000 (actual=0x00100000) "
41         "[EL--R-----]\n"
42         "ID=09         HBD 0x00305000..0x00425000 (actual=0x00120000) "
43         "[EL--------]\n"
44         "ID=10         HBI 0x00425000..0x013e5000 (actual=0x00fc0000) "
45         "[EL--R-----]\n"
46         "ID=11         SBE 0x013e5000..0x014a1000 (actual=0x000bc000) "
47         "[ELI-R-----]\n"
48         "ID=12       HCODE 0x014a1000..0x015c1000 (actual=0x00120000) "
49         "[EL--R-----]\n"
50         "ID=13        HBRT 0x015c1000..0x01bc1000 (actual=0x00600000) "
51         "[EL--R-----]\n"
52         "ID=14     PAYLOAD 0x01bc1000..0x01cc1000 (actual=0x00100000) "
53         "[-L--R-----]\n"
54         "ID=15  BOOTKERNEL 0x01cc1000..0x02bc1000 (actual=0x00f00000) "
55         "[-L--R-----]\n"
56         "ID=16         OCC 0x02bc1000..0x02ce1000 (actual=0x00120000) "
57         "[EL--R-----]\n"
58         "ID=17     FIRDATA 0x02ce1000..0x02ce4000 (actual=0x00003000) "
59         "[E-----F-C-]\n"
60         "ID=18        CAPP 0x02ce4000..0x02d08000 (actual=0x00024000) "
61         "[EL--R-----]\n"
62         "ID=19     BMC_INV 0x02d08000..0x02d11000 (actual=0x00009000) "
63         "[------F---]\n"
64         "ID=20        HBBL 0x02d11000..0x02d18000 (actual=0x00007000) "
65         "[EL--R-----]\n"
66         "ID=21    ATTR_TMP 0x02d18000..0x02d20000 (actual=0x00008000) "
67         "[------F---]\n"
68         "ID=22   ATTR_PERM 0x02d20000..0x02d28000 (actual=0x00008000) "
69         "[E-----F-C-]\n"
70         "ID=23     VERSION 0x02d28000..0x02d2a000 (actual=0x00002000) "
71         "[-L--R-----]\n"
72         "ID=24 IMA_CATALOG 0x02d2a000..0x02d6a000 (actual=0x00040000) "
73         "[EL--R-----]\n"
74         "ID=25     RINGOVD 0x02d6a000..0x02d8a000 (actual=0x00020000) "
75         "[----------]\n"
76         "ID=26     WOFDATA 0x02d8a000..0x0308a000 (actual=0x00300000) "
77         "[EL--R-----]\n"
78         "ID=27 HB_VOLATILE 0x0308a000..0x0308f000 (actual=0x00005000) "
79         "[E-----F-CV]\n"
80         "ID=28        MEMD 0x0308f000..0x0309d000 (actual=0x0000e000) "
81         "[EL--R-----]\n"
82         "ID=29        SBKT 0x0309d000..0x030a1000 (actual=0x00004000) "
83         "[EL--R-----]\n"
84         "ID=30        HDAT 0x030a1000..0x030a9000 (actual=0x00008000) "
85         "[EL--R-----]\n"
86         "ID=31      UVISOR 0x030a9000..0x031a9000 (actual=0x00100000) "
87         "[-L--R-----]\n"
88         "ID=32      OCMBFW 0x031a9000..0x031f4000 (actual=0x0004b000) "
89         "[EL--R-----]\n"
90         "ID=33    UVBWLIST 0x031f4000..0x03204000 (actual=0x00010000) "
91         "[-L--R-----]\n"
92         "ID=34 BACKUP_PART 0x03ff7000..0x03fff000 (actual=0x00000000) "
93         "[-----B----]";
94     auto parts = utils::getPartsToClear(info);
95     EXPECT_EQ(11, parts.size());
96 
97     EXPECT_EQ("HBEL", parts[0].first);
98     EXPECT_TRUE(parts[0].second);
99 
100     EXPECT_EQ("GUARD", parts[1].first);
101     EXPECT_TRUE(parts[1].second);
102 
103     EXPECT_EQ("NVRAM", parts[2].first);
104     EXPECT_FALSE(parts[2].second);
105 
106     EXPECT_EQ("HB_VOLATILE", parts[10].first);
107     EXPECT_TRUE(parts[10].second);
108 }
109 
110 TEST(TestItemUpdaterStatic, getPartsToClearNotOK)
111 {
112     // Verify the it does not crash on malformed texts
113     constexpr auto info =
114         "0x0308a000..0x0308f000(actual=0x00005000)"
115         "[E-----F-CV]\n"  // missing ID and name with F
116         "ID=27 HB_VOLATILE 0x0308a000..0x0308f000 (actual=0x00005000) "
117         "E-----F-CV]\n"   // missing [
118         "ID=22   ATTR_PERM 0x02d20000..0x02d28000 (actual=0x00008000) "
119         "[E-----F-C-]\n"  // The only valid one
120         "ID=28        MEMD 0x0308f000..0x0309d000 (actual=0x0000e000) "
121         "[----]\n"        // missing flags
122         "SBKT 0x0309d000..0x030a1000 (actual=0x00004000) "
123         "[EL--R-----]\n"; // missing ID
124 
125     auto parts = utils::getPartsToClear(info);
126     EXPECT_EQ(1, parts.size());
127     EXPECT_EQ("ATTR_PERM", parts[0].first);
128     EXPECT_TRUE(parts[0].second);
129 }
130 
131 TEST(TestItemUpdaterStatic, getPartsToClearP8BasedOK)
132 {
133     // NOTE: P8 doesn't support CLEARECC flags.
134     constexpr auto info = R"(
135 Flash info:
136 -----------
137 Name          = /dev/mtd6
138 Total size    = 64MB	 Flags E:ECC, P:PRESERVED, R:READONLY, B:BACKUP
139 Erase granule = 64KB           F:REPROVISION, V:VOLATILE, C:CLEARECC
140 
141 TOC@0x00000000 Partitions:
142 -----------
143 ID=00            part 0x00000000..0x00001000 (actual=0x00001000) [----R-----]
144 ID=01            HBEL 0x00008000..0x0002c000 (actual=0x00024000) [E-----F---]
145 ID=02           GUARD 0x0002c000..0x00031000 (actual=0x00005000) [E--P--F---]
146 ID=03             HBD 0x00031000..0x0008b000 (actual=0x0005a000) [E---R-----]
147 ID=04          HBD_RW 0x0008b000..0x00091000 (actual=0x00006000) [E---------]
148 ID=05           DJVPD 0x00091000..0x000d9000 (actual=0x00048000) [E-----F---]
149 ID=06            MVPD 0x000d9000..0x00169000 (actual=0x00090000) [E-----F---]
150 ID=07            CVPD 0x00169000..0x001b1000 (actual=0x00048000) [E-----F---]
151 ID=08             HBI 0x001b1000..0x00751000 (actual=0x005a0000) [EL--R-----]
152 ID=09            SBEC 0x00751000..0x007e1000 (actual=0x00090000) [E-I-R-----]
153 ID=10             SBE 0x007e1000..0x00829000 (actual=0x00048000) [E-I-R-----]
154 ID=11            WINK 0x00829000..0x00949000 (actual=0x00120000) [EL--R-----]
155 ID=12            HBRT 0x00949000..0x00ca9000 (actual=0x00360000) [EL--R-----]
156 ID=13         PAYLOAD 0x00ca9000..0x00d29000 (actual=0x00080000) [----R-----]
157 ID=14      BOOTKERNEL 0x00d29000..0x01ca9000 (actual=0x00f80000) [----R-----]
158 ID=15        ATTR_TMP 0x01ca9000..0x01cb1000 (actual=0x00008000) [------F---]
159 ID=16       ATTR_PERM 0x01cb1000..0x01cb9000 (actual=0x00008000) [E-----F---]
160 ID=17             OCC 0x01cb9000..0x01dd9000 (actual=0x00120000) [E---R-----]
161 ID=18            TEST 0x01dd9000..0x01de2000 (actual=0x00009000) [E---------]
162 ID=19           NVRAM 0x01de2000..0x01e72000 (actual=0x00090000) [---P--F---]
163 ID=20         FIRDATA 0x01e72000..0x01e75000 (actual=0x00003000) [E-----F---]
164 ID=21         BMC_INV 0x01e75000..0x01e7e000 (actual=0x00009000) [------F---]
165 ID=22            CAPP 0x01e7e000..0x01ea2000 (actual=0x00024000) [E---R-----]
166 ID=23         SECBOOT 0x01ea2000..0x01ec6000 (actual=0x00024000) [E--P------]
167 ID=24     IMA_CATALOG 0x01ec6000..0x01ecf000 (actual=0x00009000) [E---R-F---]
168 ID=25             HBB 0x01f60000..0x01ff0000 (actual=0x00090000) [EL--R-----]
169 ID=26         VERSION 0x01ff7000..0x01ff8000 (actual=0x00001000) [----R-----]
170 ID=27     BACKUP_PART 0x01ff8000..0x02000000 (actual=0x00000000) [----RB----]
171 ID=28      OTHER_SIDE 0x02000000..0x02008000 (actual=0x00000000) [----RB----]
172     )";
173 
174     auto parts = utils::getPartsToClear(info);
175     EXPECT_EQ(11, parts.size());
176 
177     EXPECT_EQ("HBEL", parts[0].first);
178     EXPECT_TRUE(parts[0].second);
179 
180     EXPECT_EQ("GUARD", parts[1].first);
181     EXPECT_TRUE(parts[1].second);
182 
183     EXPECT_EQ("NVRAM", parts[7].first);
184     EXPECT_FALSE(parts[7].second);
185 
186     EXPECT_EQ("IMA_CATALOG", parts[10].first);
187     EXPECT_TRUE(parts[10].second);
188 }
189