build for difference version ubuntu
[mfgtools.git] / libuuu / rominfo.cpp
blobf5044ce5c57bda44c43711acc1a10f1b0b3f9cca
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, 15> 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 },
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{ "SPL", 0x0, ROM_INFO_HID | ROM_INFO_HID_MX6 | ROM_INFO_SPL_JUMP | ROM_INFO_HID_SDP_NO_MAX_PER_TRANS},
58 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},
61 const ROM_INFO * search_rom_info(const std::string &s)
63 for (const auto &rom_info : g_RomInfo) {
64 if (s == rom_info.m_name)
66 return &rom_info;
70 return nullptr;
73 const ROM_INFO * search_rom_info(const ConfigItem *item)
75 if (item == nullptr)
77 return nullptr;
80 const ROM_INFO * const p = search_rom_info(item->m_chip);
81 if (p)
83 return p;
86 return search_rom_info(item->m_compatible);
90 #define IV_MAX_LEN 32
91 #define HASH_MAX_LEN 64
93 #define CONTAINER_HDR_ALIGNMENT 0x400
94 static constexpr uint8_t CONTAINER_TAG = 0x87;
96 #pragma pack (1)
97 struct rom_container {
98 uint8_t version;
99 uint8_t length_l;
100 uint8_t length_m;
101 uint8_t tag;
102 uint32_t flags;
103 uint16_t sw_version;
104 uint8_t fuse_version;
105 uint8_t num_images;
106 uint16_t sig_blk_offset;
107 uint16_t reserved;
110 struct rom_bootimg {
111 uint32_t offset;
112 uint32_t size;
113 uint64_t destination;
114 uint64_t entry;
115 uint32_t flags;
116 uint32_t meta;
117 uint8_t hash[HASH_MAX_LEN];
118 uint8_t iv[IV_MAX_LEN];
122 static constexpr uint32_t IMG_V2X = 0x0B;
124 #pragma pack ()
127 size_t GetContainerActualSize(shared_ptr<DataBuffer> p, size_t offset, bool bROMAPI)
129 if(bROMAPI)
130 return p->size() - offset;
132 auto hdr = reinterpret_cast<struct rom_container *>(p->data() + offset + CONTAINER_HDR_ALIGNMENT);
133 if (hdr->tag != CONTAINER_TAG)
135 return p->size() - offset;
138 /* Check if include V2X container*/
139 auto image = reinterpret_cast<struct rom_bootimg *>(p->data() + offset + CONTAINER_HDR_ALIGNMENT
140 + sizeof(struct rom_container));
142 unsigned int cindex = 1;
143 if ((image->flags & 0xF) == IMG_V2X)
145 cindex = 2;
146 hdr = reinterpret_cast<struct rom_container *>(p->data() + offset + cindex * CONTAINER_HDR_ALIGNMENT);
147 if (hdr->tag != CONTAINER_TAG)
149 return p->size() - offset;
153 image = reinterpret_cast<struct rom_bootimg *>(p->data() + offset + cindex * CONTAINER_HDR_ALIGNMENT
154 + sizeof(struct rom_container)
155 + sizeof(struct rom_bootimg) * (hdr->num_images - 1));
157 uint32_t sz = image->size + image->offset + cindex * CONTAINER_HDR_ALIGNMENT;
159 sz = round_up(sz, static_cast<uint32_t>(CONTAINER_HDR_ALIGNMENT));
161 if (sz > (p->size() - offset))
163 return p->size() - offset;
166 return sz;
169 bool CheckHeader(uint32_t *p)
171 static constexpr std::array <uint32_t, 2> FlashHeaderMagic
173 0xc0ffee01,
174 0x42464346
177 for (const auto magic_val : FlashHeaderMagic)
179 if (*p == magic_val)
181 return true;
185 return false;
188 size_t GetFlashHeaderSize(shared_ptr<DataBuffer> p, size_t offset)
190 static constexpr std::array<size_t, 4> offsets
193 0x400,
194 0x1fc,
195 0x5fc
198 for (const auto test_offset : offsets) {
199 if (p->size() < (offset + test_offset)) {
200 return 0;
203 if (CheckHeader(reinterpret_cast<uint32_t*>(p->data() + offset + test_offset))) {
204 return 0x1000;
208 return 0;
211 bool IsMBR(shared_ptr<DataBuffer> p)
213 uint16_t * m = (uint16_t *)(p->data() + 510);
214 if (*m == 0xaa55)
215 return true;
216 return false;
219 size_t ScanTerm(std::shared_ptr<DataBuffer> p, size_t &pos, size_t offset, size_t limited)
221 const char *tag = "UUUBURNXXOEUZX7+A-XY5601QQWWZ";
223 if (limited >= p->size())
224 limited = p->size();
226 limited = limited - strlen(tag) - 64;
228 if (offset > limited)
229 return 0;
231 for (size_t i = offset; i < limited; i++)
233 char *c = (char*)p->data() + i;
234 size_t length = strlen(tag);
235 size_t j;
236 for (j = 0; j < length; j++)
238 if (tag[j] != c[j])
239 break;
241 if (j == length)
243 pos = i;
244 return atoll(c + length);
248 return 0;