Add submodules checkout
[mfgtools.git] / libuuu / fastboot.h
blob64e69f645b8e9350ea66a7c7d79e286a41bfbc26
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"
36 #include <cstdint>
38 class FBFlashCmd;
39 class FileBuffer;
40 class DataBuffer;
41 class TransBase;
44 Android fastboot protocol define at
45 https://android.googlesource.com/platform/system/core/+/master/fastboot/
48 class FastBoot
50 public:
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); }
56 std::string m_info;
58 private:
59 TransBase *const m_pTrans = nullptr;
62 class FBGetVar : public CmdBase
64 public:
65 FBGetVar(char *p) :CmdBase(p) {}
67 int parser(char *p = nullptr) override;
68 int run(CmdCtx *ctx) override;
70 private:
71 std::string m_val;
72 std::string m_var;
74 friend FBFlashCmd;
77 class FBCmd: public CmdBase
79 public:
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;
86 protected:
87 std::string m_uboot_cmd;
89 private:
90 const std::string m_fb_cmd;
91 const char m_separator = ':';
94 class FBLoop : public CmdBase
96 public:
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;
106 FBLoop(char* p);
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
115 public:
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
125 public:
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
135 public:
136 FBUCmd(char *p) :FBCmd(p, "UCmd") {}
139 class FBACmd : public FBCmd
141 public:
142 FBACmd(char *p) :FBCmd(p, "ACmd") {}
145 class FBSyncCmd: public FBCmd
147 public:
148 FBSyncCmd(char *p) : FBCmd(p, "Sync") {}
151 class FBFlashingCmd : public FBCmd
153 public:
154 FBFlashingCmd(char *p) : FBCmd(p, "flashing") {}
157 class FBOemCmd : public FBCmd
159 public:
160 FBOemCmd(char *p) : FBCmd(p, "oem", ' ') {}
163 class FBFlashCmd : public FBCmd
165 public:
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);
175 private:
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
187 public:
188 FBDelPartition(char*p) : FBCmd(p, "delete-logical-partition") {}
191 class FBPartNumber : public CmdBase
193 public:
194 FBPartNumber(char *p, std::string &&fb_cmd) :CmdBase(p), m_fb_cmd{std::move(fb_cmd)}
196 m_Size = 0;
197 m_bCheckTotalParam = true;
198 m_NoKeyParam = 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;
205 private:
206 const std::string m_fb_cmd;
207 std::string m_partition_name;
208 uint32_t m_Size;
211 class FBCreatePartition : public FBPartNumber
213 public:
214 FBCreatePartition(char*p) :FBPartNumber(p, "create-logical-partition") {}
217 class FBResizePartition : public FBPartNumber
219 public:
220 FBResizePartition(char*p) :FBPartNumber(p, "resize-logical-partition") {}
223 class FBUpdateSuper : public CmdBase
225 public:
226 FBUpdateSuper(char *p) :CmdBase(p)
228 m_bCheckTotalParam = true;
229 m_NoKeyParam = 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;
236 private:
237 const std::string m_fb_cmd = "update-super";
238 std::string m_opt;
239 std::string m_partition_name;
242 class FBEraseCmd : public FBCmd
244 public:
245 FBEraseCmd(char *p) : FBCmd(p, "erase") {}
249 class FBRebootCmd : public FBCmd
251 public:
252 FBRebootCmd(char *p) : FBCmd(p, "reboot") {}
256 class FBSetActiveCmd : public FBCmd
258 public:
259 FBSetActiveCmd(char *p) : FBCmd(p, "set_active") {}
262 class FBDownload : public CmdBase
264 public:
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;
273 private:
274 std::string m_filename;
277 class FBCopy : public CmdBase
279 public:
280 FBCopy(char *p) :CmdBase(p) {}
281 int parser(char *p = nullptr) override;
282 int run(CmdCtx *ctx) override;
284 private:
285 bool m_bDownload;
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
293 public:
294 FBContinueCmd(char *p) : FBCmd(p, "continue") {}
297 class FBUpload : public CmdBase
299 public:
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;
309 private:
310 std::string m_var;
311 std::string m_filename;