The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 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 |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 12 | * the documentation and/or other materials provided with the |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 29 | #include "bootimg_utils.h" |
| 30 | |
Elliott Hughes | aaa3b6b | 2018-01-18 16:08:24 -0800 | [diff] [blame] | 31 | #include "fastboot.h" |
| 32 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 37 | void bootimg_set_cmdline(boot_img_hdr_v1* h, const std::string& cmdline) { |
| 38 | if (cmdline.size() >= sizeof(h->cmdline)) die("command line too large: %zu", cmdline.size()); |
| 39 | strcpy(reinterpret_cast<char*>(h->cmdline), cmdline.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 40 | } |
| 41 | |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 42 | boot_img_hdr_v1* mkbootimg(void* kernel, int64_t kernel_size, void* ramdisk, int64_t ramdisk_size, |
| 43 | void* second, int64_t second_size, size_t base, |
| 44 | const boot_img_hdr_v1& src, int64_t* bootimg_size) { |
| 45 | const size_t page_mask = src.page_size - 1; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 46 | |
Fernando Lugo | 91dfaec | 2018-04-11 19:02:59 -0700 | [diff] [blame] | 47 | int64_t header_actual = (sizeof(boot_img_hdr_v1) + page_mask) & (~page_mask); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 48 | int64_t kernel_actual = (kernel_size + page_mask) & (~page_mask); |
| 49 | int64_t ramdisk_actual = (ramdisk_size + page_mask) & (~page_mask); |
| 50 | int64_t second_actual = (second_size + page_mask) & (~page_mask); |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 51 | |
Hridya Valsaraju | 7d82413 | 2018-03-30 16:15:33 -0700 | [diff] [blame] | 52 | *bootimg_size = header_actual + kernel_actual + ramdisk_actual + second_actual; |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 53 | |
Hridya Valsaraju | 7d82413 | 2018-03-30 16:15:33 -0700 | [diff] [blame] | 54 | boot_img_hdr_v1* hdr = reinterpret_cast<boot_img_hdr_v1*>(calloc(*bootimg_size, 1)); |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 55 | if (hdr == nullptr) die("couldn't allocate boot image: %" PRId64 " bytes", *bootimg_size); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 56 | |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 57 | *hdr = src; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 58 | memcpy(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE); |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 59 | |
Brian Swetland | 2a63bb7 | 2009-04-28 16:05:07 -0700 | [diff] [blame] | 60 | hdr->kernel_size = kernel_size; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 61 | hdr->ramdisk_size = ramdisk_size; |
Brian Swetland | 2a63bb7 | 2009-04-28 16:05:07 -0700 | [diff] [blame] | 62 | hdr->second_size = second_size; |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 63 | |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 64 | hdr->kernel_addr += base; |
| 65 | hdr->ramdisk_addr += base; |
| 66 | hdr->second_addr += base; |
| 67 | hdr->tags_addr += base; |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 68 | |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 69 | if (hdr->header_version != 0) { |
Hridya Valsaraju | 7d82413 | 2018-03-30 16:15:33 -0700 | [diff] [blame] | 70 | hdr->header_size = sizeof(boot_img_hdr_v1); |
| 71 | } |
| 72 | |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 73 | memcpy(hdr->magic + hdr->page_size, kernel, kernel_size); |
| 74 | memcpy(hdr->magic + hdr->page_size + kernel_actual, ramdisk, ramdisk_size); |
| 75 | memcpy(hdr->magic + hdr->page_size + kernel_actual + ramdisk_actual, second, second_size); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 76 | return hdr; |
| 77 | } |