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.
44 Android fastboot protocol define at
45 https://android.googlesource.com/platform/system/core/+/master/fastboot/
51 FastBoot(TransBase
*p
) : m_pTrans
{p
} {}
53 int Transport(std::string cmd
, void *p
= nullptr, size_t size
= 0, std::vector
<uint8_t> *input
= nullptr);
54 int Transport(std::string cmd
, std::vector
<uint8_t> data
, std::vector
<uint8_t> *input
= nullptr) { return Transport(cmd
, data
.data(), data
.size(), input
); }
59 TransBase
*const m_pTrans
= nullptr;
62 class FBGetVar
: public CmdBase
65 FBGetVar(char *p
) :CmdBase(p
) {}
67 int parser(char *p
= nullptr) override
;
68 int run(CmdCtx
*ctx
) override
;
77 class FBCmd
: public CmdBase
80 FBCmd(char *p
, std::string
&&fb_cmd
, char separator
=':') :
81 CmdBase(p
), m_fb_cmd
{std::move(fb_cmd
)}, m_separator(separator
) {}
83 int parser(char *p
= nullptr) override
;
84 int run(CmdCtx
*ctx
) override
;
87 std::string m_uboot_cmd
;
90 const std::string m_fb_cmd
;
91 const char m_separator
= ':';
94 class FBLoop
: public CmdBase
97 std::string m_uboot_cmd
= "mmc read $loadaddr";
98 size_t m_blksize
= 512;
99 size_t m_each
= 0x4000000; //byte address
100 size_t m_seek
= 0; //byte address
101 size_t m_skip
= 0; //byte address
102 bool m_nostop
= false;
104 std::string m_filename
;
108 virtual int each(FastBoot
& fb
, std::shared_ptr
<DataBuffer
> fbuff
, size_t off
) = 0;
109 int run(CmdCtx
* ctx
) override
;
110 std::string
build_cmd(std::string
& cmd
, size_t off
, size_t sz
);
113 class FBCRC
: public FBLoop
116 int each(FastBoot
& fb
, std::shared_ptr
<DataBuffer
> fbuff
, size_t off
) override
;
117 FBCRC(char* p
) : FBLoop(p
) {
118 m_uboot_cmd
= "mmc read $loadaddr @off @size";
119 insert_param_info("CRC", nullptr, Param::Type::e_null
);
123 class FBWrite
: public FBLoop
126 int each(FastBoot
& fb
, std::shared_ptr
<DataBuffer
> fbuff
, size_t off
) override
;
127 FBWrite(char* p
) : FBLoop(p
) {
128 m_uboot_cmd
= "mmc write ${fastboot_buffer} @off @size";
129 insert_param_info("WRITE", nullptr, Param::Type::e_null
);
133 class FBUCmd
: public FBCmd
136 FBUCmd(char *p
) :FBCmd(p
, "UCmd") {}
139 class FBACmd
: public FBCmd
142 FBACmd(char *p
) :FBCmd(p
, "ACmd") {}
145 class FBSyncCmd
: public FBCmd
148 FBSyncCmd(char *p
) : FBCmd(p
, "Sync") {}
151 class FBFlashingCmd
: public FBCmd
154 FBFlashingCmd(char *p
) : FBCmd(p
, "flashing") {}
157 class FBOemCmd
: public FBCmd
160 FBOemCmd(char *p
) : FBCmd(p
, "oem", ' ') {}
163 class FBFlashCmd
: public FBCmd
166 FBFlashCmd(char *p
) : FBCmd(p
, "flash") { m_timeout
= 10000; }
167 int parser(char *p
= nullptr) override
;
168 int run(CmdCtx
*ctx
) override
;
169 int flash(FastBoot
*fb
, void *p
, size_t sz
);
170 int flash_raw2sparse(FastBoot
*fb
, std::shared_ptr
<FileBuffer
> p
, size_t blksz
, size_t max
);
171 bool isffu(std::shared_ptr
<FileBuffer
> p
);
172 int flash_ffu(FastBoot
*fb
, std::shared_ptr
<FileBuffer
> p
);
173 int flash_ffu_oneblk(FastBoot
*fb
, std::shared_ptr
<FileBuffer
> p
, size_t off
, size_t blksz
, size_t blkindex
);
176 std::string m_filename
;
177 std::string m_partition
;
178 bool m_raw2sparse
= false;
179 size_t m_sparse_limit
= 0x1000000;
180 uint64_t m_totalsize
;
181 bool m_scanterm
= false;
182 uint64_t m_scan_limited
= UINT64_MAX
;
185 class FBDelPartition
: public FBCmd
188 FBDelPartition(char*p
) : FBCmd(p
, "delete-logical-partition") {}
191 class FBPartNumber
: public CmdBase
194 FBPartNumber(char *p
, std::string
&&fb_cmd
) :CmdBase(p
), m_fb_cmd
{std::move(fb_cmd
)}
197 m_bCheckTotalParam
= true;
199 insert_param_info(nullptr, &m_partition_name
, Param::Type::e_string
, false, "partition name");
200 insert_param_info(nullptr, &m_Size
, Param::Type::e_uint32
, false, "partition size");
203 int run(CmdCtx
*ctx
) override
;
206 const std::string m_fb_cmd
;
207 std::string m_partition_name
;
211 class FBCreatePartition
: public FBPartNumber
214 FBCreatePartition(char*p
) :FBPartNumber(p
, "create-logical-partition") {}
217 class FBResizePartition
: public FBPartNumber
220 FBResizePartition(char*p
) :FBPartNumber(p
, "resize-logical-partition") {}
223 class FBUpdateSuper
: public CmdBase
226 FBUpdateSuper(char *p
) :CmdBase(p
)
228 m_bCheckTotalParam
= true;
230 insert_param_info(nullptr, &m_partition_name
, Param::Type::e_string
, false, "partition name");
231 insert_param_info(nullptr, &m_opt
, Param::Type::e_string
, false, "partition size");
234 int run(CmdCtx
*ctx
) override
;
237 const std::string m_fb_cmd
= "update-super";
239 std::string m_partition_name
;
242 class FBEraseCmd
: public FBCmd
245 FBEraseCmd(char *p
) : FBCmd(p
, "erase") {}
249 class FBRebootCmd
: public FBCmd
252 FBRebootCmd(char *p
) : FBCmd(p
, "reboot") {}
256 class FBSetActiveCmd
: public FBCmd
259 FBSetActiveCmd(char *p
) : FBCmd(p
, "set_active") {}
262 class FBDownload
: public CmdBase
265 FBDownload(char *p
) :CmdBase(p
)
267 insert_param_info("download", nullptr, Param::Type::e_null
);
268 insert_param_info("-f", &m_filename
, Param::Type::e_string_filename
);
271 int run(CmdCtx
*ctx
) override
;
274 std::string m_filename
;
277 class FBCopy
: public CmdBase
280 FBCopy(char *p
) :CmdBase(p
) {}
281 int parser(char *p
= nullptr) override
;
282 int run(CmdCtx
*ctx
) override
;
286 std::string m_local_file
;
287 size_t m_Maxsize_pre_cmd
= 0x10000;
288 std::string m_target_file
;
291 class FBContinueCmd
: public FBCmd
294 FBContinueCmd(char *p
) : FBCmd(p
, "continue") {}
297 class FBUpload
: public CmdBase
300 FBUpload(char* p
) : CmdBase(p
)
302 insert_param_info("upload", nullptr, Param::Type::e_null
);
303 insert_param_info("-v", &m_var
, Param::Type::e_string
);
304 insert_param_info("-f", &m_filename
, Param::Type::e_string
);
307 int run(CmdCtx
* ctx
) override
;
311 std::string m_filename
;