fixed missing tinyxml2 header path dependency for Win32
[mfgtools.git] / libuuu / rominfo.cpp
blob89d4c4705a51ea3fbfed67f7a51b81aa605cacea
1 /*
2 * Copyright 2018 NXP.
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright notice, this
11 * list of conditions and the following disclaimer in the documentation and/or
12 * other materials provided with the distribution.
14 * Neither the name of the NXP Semiconductor nor the names of its
15 * contributors may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
32 #include "rominfo.h"
33 #include "buffer.h"
34 #include "config.h"
35 #include "libcomm.h"
36 #include <cstring>
38 #include <array>
40 using namespace std;
42 static constexpr std::array<ROM_INFO, 16> g_RomInfo
44 ROM_INFO{ "MX6Q", 0x00910000, ROM_INFO_HID | ROM_INFO_HID_MX6 },
45 ROM_INFO{ "MX6D", 0x00910000, ROM_INFO_HID | ROM_INFO_HID_MX6 },
46 ROM_INFO{ "MX6SL", 0x00910000, ROM_INFO_HID | ROM_INFO_HID_MX6 },
47 ROM_INFO{ "MX7D", 0x00911000, ROM_INFO_HID | ROM_INFO_HID_MX6 | ROM_INFO_HID_SKIP_DCD },
48 ROM_INFO{ "MX6UL", 0x00910000, ROM_INFO_HID | ROM_INFO_HID_MX6 | ROM_INFO_HID_SKIP_DCD },
49 ROM_INFO{ "MX6ULL", 0x00910000, ROM_INFO_HID | ROM_INFO_HID_MX6 | ROM_INFO_HID_SKIP_DCD },
50 ROM_INFO{ "MX6SLL", 0x00910000, ROM_INFO_HID | ROM_INFO_HID_MX6 | ROM_INFO_HID_SKIP_DCD },
51 ROM_INFO{ "MX8MQ", 0x00910000, ROM_INFO_HID | ROM_INFO_HID_MX6 | ROM_INFO_HID_SKIP_DCD | ROM_INFO_NEED_BAREBOX_FULL_IMAGE},
52 ROM_INFO{ "MX7ULP", 0x2f018000, ROM_INFO_HID | ROM_INFO_HID_MX6 | ROM_INFO_HID_SKIP_DCD },
53 ROM_INFO{ "MXRT106X", 0x1000, ROM_INFO_HID | ROM_INFO_HID_MX6 | ROM_INFO_HID_SKIP_DCD },
54 ROM_INFO{ "MX8QXP", 0x0, ROM_INFO_HID | ROM_INFO_HID_NO_CMD | ROM_INFO_HID_UID_STRING },
55 ROM_INFO{ "MX28", 0x0, ROM_INFO_HID},
56 ROM_INFO{ "MX815", 0x0, ROM_INFO_HID | ROM_INFO_HID_NO_CMD | ROM_INFO_HID_UID_STRING | ROM_INFO_HID_EP1 | ROM_INFO_HID_PACK_SIZE_1020 | ROM_INFO_HID_ROMAPI},
57 ROM_INFO{ "MX95", 0x0, ROM_INFO_HID | ROM_INFO_HID_NO_CMD | ROM_INFO_HID_UID_STRING | ROM_INFO_HID_EP1 | ROM_INFO_HID_PACK_SIZE_1020 },
58 ROM_INFO{ "SPL", 0x0, ROM_INFO_HID | ROM_INFO_HID_MX6 | ROM_INFO_SPL_JUMP | ROM_INFO_HID_SDP_NO_MAX_PER_TRANS},
59 ROM_INFO{ "SPL1", 0x0, ROM_INFO_HID | ROM_INFO_HID_MX6 | ROM_INFO_SPL_JUMP | ROM_INFO_HID_SDP_NO_MAX_PER_TRANS | ROM_INFO_AUTO_SCAN_UBOOT_POS},
62 const ROM_INFO * search_rom_info(const std::string &s)
64 for (const auto &rom_info : g_RomInfo) {
65 if (s == rom_info.m_name)
67 return &rom_info;
71 return nullptr;
74 const ROM_INFO * search_rom_info(const ConfigItem *item)
76 if (item == nullptr)
78 return nullptr;
81 const ROM_INFO * const p = search_rom_info(item->m_chip);
82 if (p)
84 return p;
87 return search_rom_info(item->m_compatible);
91 #define IV_MAX_LEN 32
92 #define HASH_MAX_LEN 64
94 #define CONTAINER_HDR_ALIGNMENT 0x400
95 static constexpr uint8_t CONTAINER_TAG = 0x87;
97 #pragma pack (1)
98 struct rom_container {
99 uint8_t version;
100 uint8_t length_l;
101 uint8_t length_m;
102 uint8_t tag;
103 uint32_t flags;
104 uint16_t sw_version;
105 uint8_t fuse_version;
106 uint8_t num_images;
107 uint16_t sig_blk_offset;
108 uint16_t reserved;
111 struct rom_bootimg {
112 uint32_t offset;
113 uint32_t size;
114 uint64_t destination;
115 uint64_t entry;
116 uint32_t flags;
117 uint32_t meta;
118 uint8_t hash[HASH_MAX_LEN];
119 uint8_t iv[IV_MAX_LEN];
123 static constexpr uint32_t IMG_V2X = 0x0B;
125 #pragma pack ()
128 size_t GetContainerActualSize(shared_ptr<DataBuffer> p, size_t offset, bool bROMAPI)
130 if(bROMAPI)
131 return p->size() - offset;
133 auto hdr = reinterpret_cast<struct rom_container *>(p->data() + offset + CONTAINER_HDR_ALIGNMENT);
134 if (hdr->tag != CONTAINER_TAG)
136 return p->size() - offset;
139 /* Check if include V2X container*/
140 auto image = reinterpret_cast<struct rom_bootimg *>(p->data() + offset + CONTAINER_HDR_ALIGNMENT
141 + sizeof(struct rom_container));
143 unsigned int cindex = 1;
144 if ((image->flags & 0xF) == IMG_V2X)
146 cindex = 2;
147 hdr = reinterpret_cast<struct rom_container *>(p->data() + offset + cindex * CONTAINER_HDR_ALIGNMENT);
148 if (hdr->tag != CONTAINER_TAG)
150 return p->size() - offset;
154 image = reinterpret_cast<struct rom_bootimg *>(p->data() + offset + cindex * CONTAINER_HDR_ALIGNMENT
155 + sizeof(struct rom_container)
156 + sizeof(struct rom_bootimg) * (hdr->num_images - 1));
158 uint32_t sz = image->size + image->offset + cindex * CONTAINER_HDR_ALIGNMENT;
160 sz = round_up(sz, static_cast<uint32_t>(CONTAINER_HDR_ALIGNMENT));
162 if (sz > (p->size() - offset))
164 return p->size() - offset;
167 return sz;
170 bool CheckHeader(uint32_t *p)
172 static constexpr std::array <uint32_t, 2> FlashHeaderMagic
174 0xc0ffee01,
175 0x42464346
178 for (const auto magic_val : FlashHeaderMagic)
180 if (*p == magic_val)
182 return true;
186 return false;
189 size_t GetFlashHeaderSize(shared_ptr<DataBuffer> p, size_t offset)
191 static constexpr std::array<size_t, 4> offsets
194 0x400,
195 0x1fc,
196 0x5fc
199 for (const auto test_offset : offsets) {
200 if (p->size() < (offset + test_offset)) {
201 return 0;
204 if (CheckHeader(reinterpret_cast<uint32_t*>(p->data() + offset + test_offset))) {
205 return 0x1000;
209 return 0;
212 bool IsMBR(shared_ptr<DataBuffer> p)
214 uint16_t * m = (uint16_t *)(p->data() + 510);
215 if (*m == 0xaa55)
216 return true;
217 return false;
220 size_t ScanTerm(std::shared_ptr<DataBuffer> p, size_t &pos, size_t offset, size_t limited)
222 const char *tag = "UUUBURNXXOEUZX7+A-XY5601QQWWZ";
224 if (limited >= p->size())
225 limited = p->size();
227 limited = limited - strlen(tag) - 64;
229 if (offset > limited)
230 return 0;
232 for (size_t i = offset; i < limited; i++)
234 char *c = (char*)p->data() + i;
235 size_t length = strlen(tag);
236 size_t j;
237 for (j = 0; j < length; j++)
239 if (tag[j] != c[j])
240 break;
242 if (j == length)
244 pos = i;
245 return atoll(c + length);
249 return 0;