Update README.md
[mfgtools.git] / libuuu / sdps.cpp
blobd7f778dba37d9360622dc4742c5d154182edc2cc
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.
31 #include <string>
32 #include "sdps.h"
33 #include "hidreport.h"
34 #include "liberror.h"
35 #include "libcomm.h"
36 #include "buffer.h"
37 #include "sdp.h"
38 #include "trans.h"
39 #include <libusb.h>
41 #include <cstring>
43 //------------------------------------------------------------------------------
44 // HID Command Block Wrapper (CBW)
45 //------------------------------------------------------------------------------
46 #pragma pack (1)
48 typedef struct _CDBHIDDOWNLOAD {
49 uint8_t Command;
50 uint32_t Length;
51 uint8_t Reserved[11];
52 } CDBHIDDOWNLOAD, *PCDBHIDDOWNLOAD;
55 struct _ST_HID_CBW
57 uint32_t Signature; // Signature: 0x43544C42:1129598018, o "BLTC" (little endian) for the BLTC CBW
58 uint32_t Tag; // Tag: to be returned in the csw
59 uint32_t XferLength; // XferLength: number of bytes to transfer
60 uint8_t Flags; // Flags:
61 // Bit 7: direction - device shall ignore this bit if the
62 // XferLength field is zero, otherwise:
63 // 0 = data-out from the host to the device,
64 // 1 = data-in from the device to the host.
65 // Bits 6..0: reserved - shall be zero.
66 uint8_t Reserved[2]; // Reserved - shall be zero.
67 CDBHIDDOWNLOAD Cdb; // cdb: the command descriptor block
70 #define BLTC_DOWNLOAD_FW 2
71 #define HID_BLTC_REPORT_TYPE_DATA_OUT 2
72 #define HID_BLTC_REPORT_TYPE_COMMAND_OUT 1
74 #define CBW_BLTC_SIGNATURE 0x43544C42; // "BLTC" (little endian)
75 #define CBW_PITC_SIGNATURE 0x43544950; // "PITC" (little endian)
76 // Flags values for _ST_HID_CBW
77 #define CBW_DEVICE_TO_HOST_DIR 0x80; // "Data Out"
78 #define CBW_HOST_TO_DEVICE_DIR 0x00; // "Data In"
80 #pragma pack ()
82 #include "rominfo.h"
84 static bool is_ivt_barker_header(shared_ptr<DataBuffer> data, size_t off)
86 if (off + sizeof(IvtHeader) > data->size())
87 return false;
89 IvtHeader *p = (IvtHeader*)(data->data() + off);
90 if (p->IvtBarker == IVT_BARKER_HEADER || p->IvtBarker == IVT_BARKER2_HEADER)
91 return true;
93 return false;
96 int SDPSCmd::run(CmdCtx *pro)
98 const ROM_INFO * rom = search_rom_info(pro->m_config_item);
99 if (rom == nullptr)
101 string_ex err;
102 err.format("%s:%d can't get rom info", __FUNCTION__, __LINE__);
103 set_last_err_string(err);
104 return -1;
107 HIDTrans dev(m_timeout);
108 if (rom->flags & ROM_INFO_HID_EP1)
109 dev.set_hid_out_ep(1);
111 if(dev.open(pro->m_dev))
112 return -1;
114 shared_ptr<FileBuffer> p1 = get_file_buffer(m_filename, true);
115 if (!p1)
116 return -1;
118 shared_ptr<DataBuffer> p;
119 HIDReport report(&dev);
120 report.set_skip_notify(false);
122 size_t offset = m_offset;
124 vector<uint8_t> buff;
126 if (m_bscanterm)
128 p = p1->request_data(0, m_scan_limited);
129 if (!p)
130 return -1;
132 if (IsMBR(p))
134 size_t pos = 0, length;
135 length = ScanTerm(p, pos, 512, m_scan_limited);
136 if (length == 0)
138 set_last_err_string("This wic have NOT terminate tag after bootloader, please use new yocto");
139 return -1;
142 offset = pos - length;
143 if (ssize_t(offset) < 0)
145 set_last_err_string("This wic boot length is wrong");
146 return -1;
148 p->resize(pos);
151 else
153 p = p1->request_data(0, SIZE_MAX); //request all data
154 if (!p) return -1;
157 if (m_bskipflashheader)
158 offset += GetFlashHeaderSize(p, offset);
160 // Detect barebox binaries that have the IVT header at offset 32K
161 if (!is_ivt_barker_header(p, offset) &&
162 is_ivt_barker_header(p, offset + 0x8000))
163 offset += 0x8000;
165 if (offset >= p->size())
167 set_last_err_string("Offset bigger than file size");
168 return -1;
171 size_t sz = GetContainerActualSize(p, offset, rom->flags & ROM_INFO_HID_ROMAPI);
173 if (!(rom->flags & ROM_INFO_HID_NO_CMD))
175 _ST_HID_CBW cbw;
176 uint32_t length = (uint32_t) sz;
178 memset(&cbw, 0, sizeof(_ST_HID_CBW));
179 cbw.Cdb.Command = BLTC_DOWNLOAD_FW;
180 cbw.Cdb.Length = EndianSwap(length);
182 ++cbw.Tag;
183 cbw.Signature = CBW_BLTC_SIGNATURE;
184 cbw.XferLength = (uint32_t)length;
185 cbw.Flags = CBW_HOST_TO_DEVICE_DIR;
187 int ret = report.write(&cbw, sizeof(_ST_HID_CBW), HID_BLTC_REPORT_TYPE_COMMAND_OUT);
188 if (ret)
189 return ret;
192 if (rom->flags & ROM_INFO_HID_PACK_SIZE_1020)
193 report.set_out_package_size(1020);
195 int ret = report.write(p->data() + offset, sz, 2);
197 if (ret == 0)
199 SDPBootlogCmd log(nullptr);
200 log.run(pro);
203 return ret;