Update README.md
[mfgtools.git] / libuuu / fastboot.h
blob2acacee726b19659e555cf5c87c3f49889e27235
1 /*
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.
32 #pragma once
34 #include "cmd.h"
35 #include "bmap.h"
37 #include <cstdint>
39 class FBFlashCmd;
40 class FileBuffer;
41 class DataBuffer;
42 class TransBase;
45 Android fastboot protocol define at
46 https://android.googlesource.com/platform/system/core/+/master/fastboot/
49 class FastBoot
51 public:
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); }
57 std::string m_info;
59 private:
60 TransBase *const m_pTrans = nullptr;
63 class FBGetVar : public CmdBase
65 public:
66 FBGetVar(char *p) :CmdBase(p) {}
68 int parser(char *p = nullptr) override;
69 int run(CmdCtx *ctx) override;
71 private:
72 std::string m_val;
73 std::string m_var;
75 friend FBFlashCmd;
78 class FBCmd: public CmdBase
80 public:
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;
87 protected:
88 std::string m_uboot_cmd;
90 private:
91 const std::string m_fb_cmd;
92 const char m_separator = ':';
95 class FBLoop : public CmdBase
97 public:
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;
107 FBLoop(char* p);
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
116 public:
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
126 public:
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
136 public:
137 FBUCmd(char *p) :FBCmd(p, "UCmd") {}
140 class FBACmd : public FBCmd
142 public:
143 FBACmd(char *p) :FBCmd(p, "ACmd") {}
146 class FBSyncCmd: public FBCmd
148 public:
149 FBSyncCmd(char *p) : FBCmd(p, "Sync") {}
152 class FBFlashingCmd : public FBCmd
154 public:
155 FBFlashingCmd(char *p) : FBCmd(p, "flashing") {}
158 class FBOemCmd : public FBCmd
160 public:
161 FBOemCmd(char *p) : FBCmd(p, "oem", ' ') {}
164 class FBFlashCmd : public FBCmd
166 public:
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);
176 private:
177 bmap_t m_bmap;
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
191 public:
192 FBDelPartition(char*p) : FBCmd(p, "delete-logical-partition") {}
195 class FBPartNumber : public CmdBase
197 public:
198 FBPartNumber(char *p, std::string &&fb_cmd) :CmdBase(p), m_fb_cmd{std::move(fb_cmd)}
200 m_Size = 0;
201 m_bCheckTotalParam = true;
202 m_NoKeyParam = 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;
209 private:
210 const std::string m_fb_cmd;
211 std::string m_partition_name;
212 uint32_t m_Size;
215 class FBCreatePartition : public FBPartNumber
217 public:
218 FBCreatePartition(char*p) :FBPartNumber(p, "create-logical-partition") {}
221 class FBResizePartition : public FBPartNumber
223 public:
224 FBResizePartition(char*p) :FBPartNumber(p, "resize-logical-partition") {}
227 class FBUpdateSuper : public CmdBase
229 public:
230 FBUpdateSuper(char *p) :CmdBase(p)
232 m_bCheckTotalParam = true;
233 m_NoKeyParam = 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;
240 private:
241 const std::string m_fb_cmd = "update-super";
242 std::string m_opt;
243 std::string m_partition_name;
246 class FBEraseCmd : public FBCmd
248 public:
249 FBEraseCmd(char *p) : FBCmd(p, "erase") {}
253 class FBRebootCmd : public FBCmd
255 public:
256 FBRebootCmd(char *p) : FBCmd(p, "reboot") {}
260 class FBSetActiveCmd : public FBCmd
262 public:
263 FBSetActiveCmd(char *p) : FBCmd(p, "set_active") {}
266 class FBDownload : public CmdBase
268 public:
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;
277 private:
278 std::string m_filename;
281 class FBCopy : public CmdBase
283 public:
284 FBCopy(char *p) :CmdBase(p) {}
285 int parser(char *p = nullptr) override;
286 int run(CmdCtx *ctx) override;
288 private:
289 bool m_bDownload;
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
297 public:
298 FBBootCmd(char *p) : FBCmd(p, "boot") {}
301 class FBContinueCmd : public FBCmd
303 public:
304 FBContinueCmd(char *p) : FBCmd(p, "continue") {}
307 class FBUpload : public CmdBase
309 public:
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;
319 private:
320 std::string m_var;
321 std::string m_filename;