blob: 83e86b05d1fe5653d204df997a107aa1c9a407c7 [file] [log] [blame]
Colin Crossa3d386e2013-02-06 21:03:34 -08001/*
2 * Copyright (c) 2009-2013, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google, Inc. nor the names of its contributors
15 * may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <stdlib.h>
33#include <unistd.h>
34#include <sys/types.h>
Szymon Starzyckib6c5f282013-07-24 17:08:04 -070035#include <sys/mman.h>
36#include <sys/stat.h>
37#include <unistd.h>
38#include <sys/reboot.h>
39#include <fcntl.h>
Colin Crossa3d386e2013-02-06 21:03:34 -080040
41#include "bootimg.h"
Szymon Starzyckib6c5f282013-07-24 17:08:04 -070042#include "commands/boot.h"
43#include "commands/flash.h"
44#include "commands/partitions.h"
45#include "commands/virtual_partitions.h"
Colin Crossa3d386e2013-02-06 21:03:34 -080046#include "debug.h"
47#include "protocol.h"
Szymon Starzyckib6c5f282013-07-24 17:08:04 -070048#include "trigger.h"
49#include "utils.h"
50
51#define ATAGS_LOCATION "/proc/atags"
Colin Crossa3d386e2013-02-06 21:03:34 -080052
53static void cmd_boot(struct protocol_handle *phandle, const char *arg)
54{
Colin Crossa3d386e2013-02-06 21:03:34 -080055 unsigned kernel_actual;
56 unsigned ramdisk_actual;
Szymon Starzyckib6c5f282013-07-24 17:08:04 -070057 unsigned second_actual;
58 void *kernel_ptr;
59 void *ramdisk_ptr;
60 void *second_ptr;
61 struct boot_img_hdr *hdr;
Colin Crossb80e4c72013-09-27 20:34:32 +000062 int sz, atags_sz, new_atags_sz;
63 int rv;
64 char *ptr = NULL, *atags_ptr = NULL, *new_atags = NULL;
Szymon Starzyckib6c5f282013-07-24 17:08:04 -070065 D(DEBUG, "cmd_boot %s\n", arg);
Colin Crossa3d386e2013-02-06 21:03:34 -080066
Szymon Starzyckib6c5f282013-07-24 17:08:04 -070067 if (phandle->download_fd < 0) {
68 fastboot_fail(phandle, "no kernel file");
69 return;
70 }
71
72 atags_ptr = read_atags(ATAGS_LOCATION, &atags_sz);
73 if (atags_ptr == NULL) {
74 fastboot_fail(phandle, "atags read error");
75 goto error;
76 }
77
Colin Crossb80e4c72013-09-27 20:34:32 +000078 sz = get_file_size(phandle->download_fd);
Szymon Starzyckib6c5f282013-07-24 17:08:04 -070079 ptr = (char *) mmap(NULL, sz, PROT_READ,
Colin Crossb80e4c72013-09-27 20:34:32 +000080 MAP_POPULATE | MAP_PRIVATE, phandle->download_fd, 0);
Szymon Starzyckib6c5f282013-07-24 17:08:04 -070081 hdr = (struct boot_img_hdr *) ptr;
82
83 if (ptr == MAP_FAILED) {
84 fastboot_fail(phandle, "internal fastbootd error");
85 goto error;
86 }
87
88 if ((size_t) sz < sizeof(*hdr)) {
Colin Crossa3d386e2013-02-06 21:03:34 -080089 fastboot_fail(phandle, "invalid bootimage header");
Szymon Starzyckib6c5f282013-07-24 17:08:04 -070090 goto error;
Colin Crossa3d386e2013-02-06 21:03:34 -080091 }
92
Szymon Starzyckib6c5f282013-07-24 17:08:04 -070093 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, hdr->page_size);
94 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, hdr->page_size);
95 second_actual = ROUND_TO_PAGE(hdr->second_size, hdr->page_size);
Colin Crossa3d386e2013-02-06 21:03:34 -080096
Szymon Starzyckib6c5f282013-07-24 17:08:04 -070097 new_atags = (char *) create_atags((unsigned *) atags_ptr, atags_sz, hdr, &new_atags_sz);
Colin Crossa3d386e2013-02-06 21:03:34 -080098
Szymon Starzyckib6c5f282013-07-24 17:08:04 -070099 if (new_atags == NULL) {
100 fastboot_fail(phandle, "atags generate error");
101 goto error;
102 }
103 if (new_atags_sz > 0x4000) {
104 fastboot_fail(phandle, "atags file to large");
105 goto error;
106 }
Colin Crossa3d386e2013-02-06 21:03:34 -0800107
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700108 if ((int) (hdr->page_size + kernel_actual + ramdisk_actual) < sz) {
Colin Crossa3d386e2013-02-06 21:03:34 -0800109 fastboot_fail(phandle, "incomplete bootimage");
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700110 goto error;
Colin Crossa3d386e2013-02-06 21:03:34 -0800111 }
112
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700113 kernel_ptr = (void *)((unsigned) ptr + hdr->page_size);
114 ramdisk_ptr = (void *)((unsigned) kernel_ptr + kernel_actual);
115 second_ptr = (void *)((unsigned) ramdisk_ptr + ramdisk_actual);
116
117 D(INFO, "preparing to boot");
118 // Prepares boot physical addresses from header are ignored
119 rv = prepare_boot_linux(hdr->kernel_addr, kernel_ptr, kernel_actual,
120 hdr->ramdisk_addr, ramdisk_ptr, ramdisk_actual,
121 hdr->second_addr, second_ptr, second_actual,
122 hdr->tags_addr, new_atags, ROUND_TO_PAGE(new_atags_sz, hdr->page_size));
123 if (rv < 0) {
124 fastboot_fail(phandle, "kexec prepare failed");
125 goto error;
126 }
Colin Crossa3d386e2013-02-06 21:03:34 -0800127
128 fastboot_okay(phandle, "");
Colin Crossa3d386e2013-02-06 21:03:34 -0800129
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700130 free(atags_ptr);
131 munmap(ptr, sz);
132 free(new_atags);
Colin Crossa3d386e2013-02-06 21:03:34 -0800133
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700134 D(INFO, "Kexec going to reboot");
135 reboot(LINUX_REBOOT_CMD_KEXEC);
136
137 fastboot_fail(phandle, "reboot error");
138
139 return;
140
141error:
142
143 if (atags_ptr != NULL)
144 free(atags_ptr);
145 if (ptr != NULL)
146 munmap(ptr, sz);
147
Colin Crossa3d386e2013-02-06 21:03:34 -0800148}
149
150static void cmd_erase(struct protocol_handle *phandle, const char *arg)
151{
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700152 int partition_fd;
153 char path[PATH_MAX];
154 D(DEBUG, "cmd_erase %s\n", arg);
Colin Crossa3d386e2013-02-06 21:03:34 -0800155
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700156 if (flash_find_entry(arg, path, PATH_MAX)) {
Colin Crossa3d386e2013-02-06 21:03:34 -0800157 fastboot_fail(phandle, "partition table doesn't exist");
158 return;
159 }
160
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700161 if (path == NULL) {
162 fastboot_fail(phandle, "Couldn't find partition");
Colin Crossa3d386e2013-02-06 21:03:34 -0800163 return;
164 }
165
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700166 partition_fd = flash_get_partiton(path);
167 if (partition_fd < 0) {
168 fastboot_fail(phandle, "partiton file does not exists");
169 }
170
171 if (flash_erase(partition_fd)) {
172 fastboot_fail(phandle, "failed to erase partition");
173 flash_close(partition_fd);
174 return;
175 }
176
177 if (flash_close(partition_fd) < 0) {
178 D(ERR, "could not close device %s", strerror(errno));
Colin Crossa3d386e2013-02-06 21:03:34 -0800179 fastboot_fail(phandle, "failed to erase partition");
180 return;
181 }
182 fastboot_okay(phandle, "");
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700183}
184
185static int GPT_header_location() {
186 const char *location_str = fastboot_getvar("gpt_sector");
187 char *str;
188 int location;
189
190 if (!strcmp("", location_str)) {
191 D(INFO, "GPT location not specified using second sector");
192 return 1;
193 }
194 else {
195 location = strtoul(location_str, &str, 10);
196 D(INFO, "GPT location specified as %d", location);
197
198 if (*str != '\0')
199 return -1;
200
201 return location - 1;
202 }
203}
204
205static void cmd_gpt_layout(struct protocol_handle *phandle, const char *arg) {
206 struct GPT_entry_table *oldtable;
207 int location;
208 struct GPT_content content;
209 const char *device;
210 device = fastboot_getvar("blockdev");
211
212 if (!strcmp(device, "")) {
213 fastboot_fail(phandle, "blockdev not defined in config file");
214 return;
215 }
216
217 if (phandle->download_fd < 0) {
218 fastboot_fail(phandle, "no layout file");
219 return;
220 }
221
222 location = GPT_header_location();
223 oldtable = GPT_get_device(device, location);
224
225 GPT_default_content(&content, oldtable);
226 if (oldtable == NULL)
227 D(WARN, "Could not get old gpt table");
228 else
229 GPT_release_device(oldtable);
230
231 if (!GPT_parse_file(phandle->download_fd, &content)) {
232 fastboot_fail(phandle, "Could not parse partition config file");
233 return;
234 }
235
236 if (trigger_gpt_layout(&content)) {
237 fastboot_fail(phandle, "Vendor forbids this opperation");
238 GPT_release_content(&content);
239 return;
240 }
241
242 if (!GPT_write_content(device, &content)) {
243 fastboot_fail(phandle, "Unable to write gpt file");
244 GPT_release_content(&content);
245 return;
246 }
247
248 GPT_release_content(&content);
249 fastboot_okay(phandle, "");
Colin Crossa3d386e2013-02-06 21:03:34 -0800250}
251
252static void cmd_flash(struct protocol_handle *phandle, const char *arg)
253{
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700254 int partition;
255 uint64_t sz;
256 char data[BOOT_MAGIC_SIZE];
257 char path[PATH_MAX];
258 ssize_t header_sz = 0;
Colin Crossa3d386e2013-02-06 21:03:34 -0800259
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700260 D(DEBUG, "cmd_flash %s\n", arg);
261
262 if (try_handle_virtual_partition(phandle, arg)) {
263 return;
264 }
265
266 if (phandle->download_fd < 0) {
267 fastboot_fail(phandle, "no kernel file");
268 return;
269 }
270
271 if (flash_find_entry(arg, path, PATH_MAX)) {
Colin Crossa3d386e2013-02-06 21:03:34 -0800272 fastboot_fail(phandle, "partition table doesn't exist");
273 return;
274 }
275
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700276 // TODO: Maybe its goot idea to check whether the partition is just bootable partition
277 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
Colin Crossb80e4c72013-09-27 20:34:32 +0000278 if (read_data_once(phandle->download_fd, data, BOOT_MAGIC_SIZE) < BOOT_MAGIC_SIZE) {
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700279 fastboot_fail(phandle, "incoming data read error, cannot read boot header");
280 return;
281 }
Colin Crossa3d386e2013-02-06 21:03:34 -0800282 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
283 fastboot_fail(phandle, "image is not a boot image");
284 return;
285 }
286 }
287
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700288 partition = flash_get_partiton(path);
Colin Crossa3d386e2013-02-06 21:03:34 -0800289
Colin Crossb80e4c72013-09-27 20:34:32 +0000290 sz = get_file_size64(phandle->download_fd);
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700291 if (sz > get_file_size64(partition)) {
292 flash_close(partition);
293 D(WARN, "size of file too large");
294 fastboot_fail(phandle, "size of file too large");
295 return;
296 }
297
298 D(INFO, "writing %lld bytes to '%s'\n", sz, arg);
299
300 if (flash_write(partition, phandle->download_fd, sz, header_sz)) {
Colin Crossa3d386e2013-02-06 21:03:34 -0800301 fastboot_fail(phandle, "flash write failure");
302 return;
303 }
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700304 D(INFO, "partition '%s' updated\n", arg);
305
306 flash_close(partition);
307
Colin Crossa3d386e2013-02-06 21:03:34 -0800308 fastboot_okay(phandle, "");
309}
310
311static void cmd_continue(struct protocol_handle *phandle, const char *arg)
312{
313 fastboot_okay(phandle, "");
314#if 0
315 udc_stop();
316
317 boot_linux_from_flash();
318#endif
319}
320
321static void cmd_getvar(struct protocol_handle *phandle, const char *arg)
322{
323 const char *value;
324 D(DEBUG, "cmd_getvar %s\n", arg);
325
326 value = fastboot_getvar(arg);
327
328 fastboot_okay(phandle, value);
329}
330
331static void cmd_download(struct protocol_handle *phandle, const char *arg)
332{
333 unsigned len = strtoul(arg, NULL, 16);
334 int old_fd;
335
336 if (len > 256 * 1024 * 1024) {
337 fastboot_fail(phandle, "data too large");
338 return;
339 }
340
341 fastboot_data(phandle, len);
342
343 old_fd = protocol_get_download(phandle);
344 if (old_fd >= 0) {
345 off_t len = lseek(old_fd, 0, SEEK_END);
346 D(INFO, "disposing of unused fd %d, size %ld", old_fd, len);
347 close(old_fd);
348 }
349
350 phandle->download_fd = protocol_handle_download(phandle, len);
351 if (phandle->download_fd < 0) {
352 //handle->state = STATE_ERROR;
353 fastboot_fail(phandle, "download failed");
354 return;
355 }
356
357 fastboot_okay(phandle, "");
358}
359
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700360static void cmd_oem(struct protocol_handle *phandle, const char *arg) {
361 const char *response = "";
362
363 if (trigger_oem_cmd(arg, &response))
364 fastboot_fail(phandle, response);
365 else
366 fastboot_okay(phandle, response);
367}
368
Colin Crossa3d386e2013-02-06 21:03:34 -0800369void commands_init()
370{
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700371 virtual_partition_register("partition-table", cmd_gpt_layout);
372
Colin Crossa3d386e2013-02-06 21:03:34 -0800373 fastboot_register("boot", cmd_boot);
374 fastboot_register("erase:", cmd_erase);
375 fastboot_register("flash:", cmd_flash);
376 fastboot_register("continue", cmd_continue);
377 fastboot_register("getvar:", cmd_getvar);
378 fastboot_register("download:", cmd_download);
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700379 fastboot_register("oem", cmd_oem);
Colin Crossa3d386e2013-02-06 21:03:34 -0800380 //fastboot_publish("version", "0.5");
381 //fastboot_publish("product", "swordfish");
382 //fastboot_publish("kernel", "lk");
383}