diff options
author | Tao Bao | 2016-11-18 14:01:26 -0600 |
---|---|---|
committer | Tao Bao | 2016-11-18 14:04:48 -0600 |
commit | bedf5fc11cea9cc6b92f37597fe8624d25b8d371 (patch) | |
tree | 20e6f71aeb7ea2615f1fad19abc8f28d821079fc /bootloader_message | |
parent | 23e785935e2e373a2f4882619b35b34bcd95a1e6 (diff) | |
download | platform-bootable-recovery-bedf5fc11cea9cc6b92f37597fe8624d25b8d371.tar.gz platform-bootable-recovery-bedf5fc11cea9cc6b92f37597fe8624d25b8d371.tar.xz platform-bootable-recovery-bedf5fc11cea9cc6b92f37597fe8624d25b8d371.zip |
updater: Refactor set_stage() and get_stage() functions.
Add read_bootloader_message_from() and write_bootloader_message_to() to
allow specifying the BCB device (/misc).
Also add testcases for set_stage() and get_stage().
Test: recovery_component_test passes.
Test: Build a recovery image and apply a two-step OTA package.
Change-Id: If5ab06a1aaaea168d2a9e5dd63c07c0a3190e4ae
Diffstat (limited to 'bootloader_message')
-rw-r--r-- | bootloader_message/bootloader_message.cpp | 68 | ||||
-rw-r--r-- | bootloader_message/include/bootloader_message/bootloader_message.h | 18 |
2 files changed, 63 insertions, 23 deletions
diff --git a/bootloader_message/bootloader_message.cpp b/bootloader_message/bootloader_message.cpp index f6f8005f..9a567184 100644 --- a/bootloader_message/bootloader_message.cpp +++ b/bootloader_message/bootloader_message.cpp | |||
@@ -80,26 +80,23 @@ static bool wait_for_device(const std::string& blk_device, std::string* err) { | |||
80 | return ret == 0; | 80 | return ret == 0; |
81 | } | 81 | } |
82 | 82 | ||
83 | static bool read_misc_partition(void* p, size_t size, size_t offset, std::string* err) { | 83 | static bool read_misc_partition(void* p, size_t size, const std::string& misc_blk_device, |
84 | std::string misc_blk_device = get_misc_blk_device(err); | 84 | size_t offset, std::string* err) { |
85 | if (misc_blk_device.empty()) { | ||
86 | return false; | ||
87 | } | ||
88 | if (!wait_for_device(misc_blk_device, err)) { | 85 | if (!wait_for_device(misc_blk_device, err)) { |
89 | return false; | 86 | return false; |
90 | } | 87 | } |
91 | android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY)); | 88 | android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY)); |
92 | if (fd.get() == -1) { | 89 | if (fd == -1) { |
93 | *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(), | 90 | *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(), |
94 | strerror(errno)); | 91 | strerror(errno)); |
95 | return false; | 92 | return false; |
96 | } | 93 | } |
97 | if (lseek(fd.get(), static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) { | 94 | if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) { |
98 | *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(), | 95 | *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(), |
99 | strerror(errno)); | 96 | strerror(errno)); |
100 | return false; | 97 | return false; |
101 | } | 98 | } |
102 | if (!android::base::ReadFully(fd.get(), p, size)) { | 99 | if (!android::base::ReadFully(fd, p, size)) { |
103 | *err = android::base::StringPrintf("failed to read %s: %s", misc_blk_device.c_str(), | 100 | *err = android::base::StringPrintf("failed to read %s: %s", misc_blk_device.c_str(), |
104 | strerror(errno)); | 101 | strerror(errno)); |
105 | return false; | 102 | return false; |
@@ -107,29 +104,25 @@ static bool read_misc_partition(void* p, size_t size, size_t offset, std::string | |||
107 | return true; | 104 | return true; |
108 | } | 105 | } |
109 | 106 | ||
110 | static bool write_misc_partition(const void* p, size_t size, size_t offset, std::string* err) { | 107 | static bool write_misc_partition(const void* p, size_t size, const std::string& misc_blk_device, |
111 | std::string misc_blk_device = get_misc_blk_device(err); | 108 | size_t offset, std::string* err) { |
112 | if (misc_blk_device.empty()) { | 109 | android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY)); |
113 | return false; | 110 | if (fd == -1) { |
114 | } | ||
115 | android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY | O_SYNC)); | ||
116 | if (fd.get() == -1) { | ||
117 | *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(), | 111 | *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(), |
118 | strerror(errno)); | 112 | strerror(errno)); |
119 | return false; | 113 | return false; |
120 | } | 114 | } |
121 | if (lseek(fd.get(), static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) { | 115 | if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) { |
122 | *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(), | 116 | *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(), |
123 | strerror(errno)); | 117 | strerror(errno)); |
124 | return false; | 118 | return false; |
125 | } | 119 | } |
126 | if (!android::base::WriteFully(fd.get(), p, size)) { | 120 | if (!android::base::WriteFully(fd, p, size)) { |
127 | *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(), | 121 | *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(), |
128 | strerror(errno)); | 122 | strerror(errno)); |
129 | return false; | 123 | return false; |
130 | } | 124 | } |
131 | // TODO: O_SYNC and fsync duplicates each other? | 125 | if (fsync(fd) == -1) { |
132 | if (fsync(fd.get()) == -1) { | ||
133 | *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(), | 126 | *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(), |
134 | strerror(errno)); | 127 | strerror(errno)); |
135 | return false; | 128 | return false; |
@@ -137,12 +130,32 @@ static bool write_misc_partition(const void* p, size_t size, size_t offset, std: | |||
137 | return true; | 130 | return true; |
138 | } | 131 | } |
139 | 132 | ||
133 | bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device, | ||
134 | std::string* err) { | ||
135 | return read_misc_partition(boot, sizeof(*boot), misc_blk_device, | ||
136 | BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err); | ||
137 | } | ||
138 | |||
140 | bool read_bootloader_message(bootloader_message* boot, std::string* err) { | 139 | bool read_bootloader_message(bootloader_message* boot, std::string* err) { |
141 | return read_misc_partition(boot, sizeof(*boot), BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err); | 140 | std::string misc_blk_device = get_misc_blk_device(err); |
141 | if (misc_blk_device.empty()) { | ||
142 | return false; | ||
143 | } | ||
144 | return read_bootloader_message_from(boot, misc_blk_device, err); | ||
145 | } | ||
146 | |||
147 | bool write_bootloader_message_to(const bootloader_message& boot, const std::string& misc_blk_device, | ||
148 | std::string* err) { | ||
149 | return write_misc_partition(&boot, sizeof(boot), misc_blk_device, | ||
150 | BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err); | ||
142 | } | 151 | } |
143 | 152 | ||
144 | bool write_bootloader_message(const bootloader_message& boot, std::string* err) { | 153 | bool write_bootloader_message(const bootloader_message& boot, std::string* err) { |
145 | return write_misc_partition(&boot, sizeof(boot), BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err); | 154 | std::string misc_blk_device = get_misc_blk_device(err); |
155 | if (misc_blk_device.empty()) { | ||
156 | return false; | ||
157 | } | ||
158 | return write_bootloader_message_to(boot, misc_blk_device, err); | ||
146 | } | 159 | } |
147 | 160 | ||
148 | bool clear_bootloader_message(std::string* err) { | 161 | bool clear_bootloader_message(std::string* err) { |
@@ -177,12 +190,21 @@ bool write_reboot_bootloader(std::string* err) { | |||
177 | } | 190 | } |
178 | 191 | ||
179 | bool read_wipe_package(std::string* package_data, size_t size, std::string* err) { | 192 | bool read_wipe_package(std::string* package_data, size_t size, std::string* err) { |
193 | std::string misc_blk_device = get_misc_blk_device(err); | ||
194 | if (misc_blk_device.empty()) { | ||
195 | return false; | ||
196 | } | ||
180 | package_data->resize(size); | 197 | package_data->resize(size); |
181 | return read_misc_partition(&(*package_data)[0], size, WIPE_PACKAGE_OFFSET_IN_MISC, err); | 198 | return read_misc_partition(&(*package_data)[0], size, misc_blk_device, |
199 | WIPE_PACKAGE_OFFSET_IN_MISC, err); | ||
182 | } | 200 | } |
183 | 201 | ||
184 | bool write_wipe_package(const std::string& package_data, std::string* err) { | 202 | bool write_wipe_package(const std::string& package_data, std::string* err) { |
185 | return write_misc_partition(package_data.data(), package_data.size(), | 203 | std::string misc_blk_device = get_misc_blk_device(err); |
204 | if (misc_blk_device.empty()) { | ||
205 | return false; | ||
206 | } | ||
207 | return write_misc_partition(package_data.data(), package_data.size(), misc_blk_device, | ||
186 | WIPE_PACKAGE_OFFSET_IN_MISC, err); | 208 | WIPE_PACKAGE_OFFSET_IN_MISC, err); |
187 | } | 209 | } |
188 | 210 | ||
diff --git a/bootloader_message/include/bootloader_message/bootloader_message.h b/bootloader_message/include/bootloader_message/bootloader_message.h index 5a5dd879..e45f4248 100644 --- a/bootloader_message/include/bootloader_message/bootloader_message.h +++ b/bootloader_message/include/bootloader_message/bootloader_message.h | |||
@@ -178,15 +178,33 @@ static_assert(sizeof(struct bootloader_control) == | |||
178 | #include <string> | 178 | #include <string> |
179 | #include <vector> | 179 | #include <vector> |
180 | 180 | ||
181 | // Read bootloader message into boot. Error message will be set in err. | ||
181 | bool read_bootloader_message(bootloader_message* boot, std::string* err); | 182 | bool read_bootloader_message(bootloader_message* boot, std::string* err); |
183 | |||
184 | // Read bootloader message from the specified misc device into boot. | ||
185 | bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device, | ||
186 | std::string* err); | ||
187 | |||
188 | // Write bootloader message to BCB. | ||
182 | bool write_bootloader_message(const bootloader_message& boot, std::string* err); | 189 | bool write_bootloader_message(const bootloader_message& boot, std::string* err); |
190 | |||
191 | // Write bootloader message to the specified BCB device. | ||
192 | bool write_bootloader_message_to(const bootloader_message& boot, | ||
193 | const std::string& misc_blk_device, std::string* err); | ||
194 | |||
195 | // Write bootloader message (boots into recovery with the options) to BCB. | ||
183 | bool write_bootloader_message(const std::vector<std::string>& options, std::string* err); | 196 | bool write_bootloader_message(const std::vector<std::string>& options, std::string* err); |
197 | |||
198 | // Clear BCB. | ||
184 | bool clear_bootloader_message(std::string* err); | 199 | bool clear_bootloader_message(std::string* err); |
185 | 200 | ||
186 | // Writes the reboot-bootloader reboot reason to the bootloader_message. | 201 | // Writes the reboot-bootloader reboot reason to the bootloader_message. |
187 | bool write_reboot_bootloader(std::string* err); | 202 | bool write_reboot_bootloader(std::string* err); |
188 | 203 | ||
204 | // Read the wipe package from BCB (from offset WIPE_PACKAGE_OFFSET_IN_MISC). | ||
189 | bool read_wipe_package(std::string* package_data, size_t size, std::string* err); | 205 | bool read_wipe_package(std::string* package_data, size_t size, std::string* err); |
206 | |||
207 | // Write the wipe package into BCB (to offset WIPE_PACKAGE_OFFSET_IN_MISC). | ||
190 | bool write_wipe_package(const std::string& package_data, std::string* err); | 208 | bool write_wipe_package(const std::string& package_data, std::string* err); |
191 | 209 | ||
192 | #else | 210 | #else |