2 * Copyright 2018, 2022 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.
45 Android fastboot protocol define at
46 https://android.googlesource.com/platform/system/core/+/master/fastboot/
52 FastBoot(TransBase
*p
) : m_pTrans
{p
} {}
54 int Transport(std::string cmd
, void *p
= nullptr, size_t size
= 0, std::vector
<uint8_t> *input
= nullptr);
55 int Transport(std::string cmd
, std::vector
<uint8_t> data
, std::vector
<uint8_t> *input
= nullptr) { return Transport(cmd
, data
.data(), data
.size(), input
); }
60 TransBase
*const m_pTrans
= nullptr;
63 class FBGetVar
: public CmdBase
66 FBGetVar(char *p
) :CmdBase(p
) {}
68 int parser(char *p
= nullptr) override
;
69 int run(CmdCtx
*ctx
) override
;
78 class FBCmd
: public CmdBase
81 FBCmd(char *p
, std::string
&&fb_cmd
, char separator
=':') :
82 CmdBase(p
), m_fb_cmd
{std::move(fb_cmd
)}, m_separator(separator
) {}
84 int parser(char *p
= nullptr) override
;
85 int run(CmdCtx
*ctx
) override
;
88 std::string m_uboot_cmd
;
91 const std::string m_fb_cmd
;
92 const char m_separator
= ':';
95 class FBLoop
: public CmdBase
98 std::string m_uboot_cmd
= "mmc read $loadaddr";
99 size_t m_blksize
= 512;
100 size_t m_each
= 0x4000000; //byte address
101 size_t m_seek
= 0; //byte address
102 size_t m_skip
= 0; //byte address
103 bool m_nostop
= false;
105 std::string m_filename
;
109 virtual int each(FastBoot
& fb
, std::shared_ptr
<DataBuffer
> fbuff
, size_t off
) = 0;
110 int run(CmdCtx
* ctx
) override
;
111 std::string
build_cmd(std::string
& cmd
, size_t off
, size_t sz
);
114 class FBCRC
: public FBLoop
117 int each(FastBoot
& fb
, std::shared_ptr
<DataBuffer
> fbuff
, size_t off
) override
;
118 FBCRC(char* p
) : FBLoop(p
) {
119 m_uboot_cmd
= "mmc read $loadaddr @off @size";
120 insert_param_info("CRC", nullptr, Param::Type::e_null
);
124 class FBWrite
: public FBLoop
127 int each(FastBoot
& fb
, std::shared_ptr
<DataBuffer
> fbuff
, size_t off
) override
;
128 FBWrite(char* p
) : FBLoop(p
) {
129 m_uboot_cmd
= "mmc write ${fastboot_buffer} @off @size";
130 insert_param_info("WRITE", nullptr, Param::Type::e_null
);
134 class FBUCmd
: public FBCmd
137 FBUCmd(char *p
) :FBCmd(p
, "UCmd") {}
140 class FBACmd
: public FBCmd
143 FBACmd(char *p
) :FBCmd(p
, "ACmd") {}
146 class FBSyncCmd
: public FBCmd
149 FBSyncCmd(char *p
) : FBCmd(p
, "Sync") {}
152 class FBFlashingCmd
: public FBCmd
155 FBFlashingCmd(char *p
) : FBCmd(p
, "flashing") {}
158 class FBOemCmd
: public FBCmd
161 FBOemCmd(char *p
) : FBCmd(p
, "oem", ' ') {}
164 class FBFlashCmd
: public FBCmd
167 FBFlashCmd(char *p
) : FBCmd(p
, "flash") { m_timeout
= 10000; }
168 int parser(char *p
= nullptr) override
;
169 int run(CmdCtx
*ctx
) override
;
170 int flash(FastBoot
*fb
, void *p
, size_t sz
);
171 int flash_raw2sparse(FastBoot
*fb
, std::shared_ptr
<FileBuffer
> p
, size_t max
);
172 bool isffu(std::shared_ptr
<FileBuffer
> p
);
173 int flash_ffu(FastBoot
*fb
, std::shared_ptr
<FileBuffer
> p
);
174 int flash_ffu_oneblk(FastBoot
*fb
, std::shared_ptr
<FileBuffer
> p
, size_t off
, size_t blksz
, size_t blkindex
);
178 std::string m_filename
;
179 std::string m_bmap_filename
;
180 std::string m_partition
;
181 bool m_raw2sparse
= false;
182 bool m_use_bmap
= false;
183 size_t m_sparse_limit
= 0x1000000;
184 uint64_t m_totalsize
;
185 bool m_scanterm
= false;
186 uint64_t m_scan_limited
= UINT64_MAX
;
189 class FBDelPartition
: public FBCmd
192 FBDelPartition(char*p
) : FBCmd(p
, "delete-logical-partition") {}
195 class FBPartNumber
: public CmdBase
198 FBPartNumber(char *p
, std::string
&&fb_cmd
) :CmdBase(p
), m_fb_cmd
{std::move(fb_cmd
)}
201 m_bCheckTotalParam
= true;
203 insert_param_info(nullptr, &m_partition_name
, Param::Type::e_string
, false, "partition name");
204 insert_param_info(nullptr, &m_Size
, Param::Type::e_uint32
, false, "partition size");
207 int run(CmdCtx
*ctx
) override
;
210 const std::string m_fb_cmd
;
211 std::string m_partition_name
;
215 class FBCreatePartition
: public FBPartNumber
218 FBCreatePartition(char*p
) :FBPartNumber(p
, "create-logical-partition") {}
221 class FBResizePartition
: public FBPartNumber
224 FBResizePartition(char*p
) :FBPartNumber(p
, "resize-logical-partition") {}
227 class FBUpdateSuper
: public CmdBase
230 FBUpdateSuper(char *p
) :CmdBase(p
)
232 m_bCheckTotalParam
= true;
234 insert_param_info(nullptr, &m_partition_name
, Param::Type::e_string
, false, "partition name");
235 insert_param_info(nullptr, &m_opt
, Param::Type::e_string
, false, "partition size");
238 int run(CmdCtx
*ctx
) override
;
241 const std::string m_fb_cmd
= "update-super";
243 std::string m_partition_name
;
246 class FBEraseCmd
: public FBCmd
249 FBEraseCmd(char *p
) : FBCmd(p
, "erase") {}
253 class FBRebootCmd
: public FBCmd
256 FBRebootCmd(char *p
) : FBCmd(p
, "reboot") {}
260 class FBSetActiveCmd
: public FBCmd
263 FBSetActiveCmd(char *p
) : FBCmd(p
, "set_active") {}
266 class FBDownload
: public CmdBase
269 FBDownload(char *p
) :CmdBase(p
)
271 insert_param_info("download", nullptr, Param::Type::e_null
);
272 insert_param_info("-f", &m_filename
, Param::Type::e_string_filename
);
275 int run(CmdCtx
*ctx
) override
;
278 std::string m_filename
;
281 class FBCopy
: public CmdBase
284 FBCopy(char *p
) :CmdBase(p
) {}
285 int parser(char *p
= nullptr) override
;
286 int run(CmdCtx
*ctx
) override
;
290 std::string m_local_file
;
291 size_t m_Maxsize_pre_cmd
= 0x10000;
292 std::string m_target_file
;
295 class FBBootCmd
: public FBCmd
298 FBBootCmd(char *p
) : FBCmd(p
, "boot") {}
301 class FBContinueCmd
: public FBCmd
304 FBContinueCmd(char *p
) : FBCmd(p
, "continue") {}
307 class FBUpload
: public CmdBase
310 FBUpload(char* p
) : CmdBase(p
)
312 insert_param_info("upload", nullptr, Param::Type::e_null
);
313 insert_param_info("-v", &m_var
, Param::Type::e_string
);
314 insert_param_info("-f", &m_filename
, Param::Type::e_string
);
317 int run(CmdCtx
* ctx
) override
;
321 std::string m_filename
;