| 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 |  | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 31 | #include "util.h" | 
| Elliott Hughes | aaa3b6b | 2018-01-18 16:08:24 -0800 | [diff] [blame] | 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 |  | 
| Yi-Yo Chiang | c0c9e30 | 2021-03-23 17:48:23 +0800 | [diff] [blame] | 37 | static void bootimg_set_cmdline_v3_and_above(boot_img_hdr_v3* h, const std::string& cmdline) { | 
| Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 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 |  | 
| Steve Muckle | 15303f2 | 2020-03-18 14:55:06 -0700 | [diff] [blame] | 42 | void bootimg_set_cmdline(boot_img_hdr_v2* h, const std::string& cmdline) { | 
| Yi-Yo Chiang | c0c9e30 | 2021-03-23 17:48:23 +0800 | [diff] [blame] | 43 |     if (h->header_version >= 3) { | 
 | 44 |         return bootimg_set_cmdline_v3_and_above(reinterpret_cast<boot_img_hdr_v3*>(h), cmdline); | 
| Steve Muckle | 15303f2 | 2020-03-18 14:55:06 -0700 | [diff] [blame] | 45 |     } | 
 | 46 |     if (cmdline.size() >= sizeof(h->cmdline)) die("command line too large: %zu", cmdline.size()); | 
 | 47 |     strcpy(reinterpret_cast<char*>(h->cmdline), cmdline.c_str()); | 
 | 48 | } | 
 | 49 |  | 
| Yi-Yo Chiang | ac4a136 | 2021-03-25 01:14:52 +0800 | [diff] [blame] | 50 | static void mkbootimg_v3_and_above(const std::vector<char>& kernel, | 
 | 51 |                                    const std::vector<char>& ramdisk, const boot_img_hdr_v2& src, | 
 | 52 |                                    std::vector<char>* out) { | 
| Steve Muckle | 15303f2 | 2020-03-18 14:55:06 -0700 | [diff] [blame] | 53 | #define V3_PAGE_SIZE 4096 | 
 | 54 |     const size_t page_mask = V3_PAGE_SIZE - 1; | 
 | 55 |     int64_t kernel_actual = (kernel.size() + page_mask) & (~page_mask); | 
 | 56 |     int64_t ramdisk_actual = (ramdisk.size() + page_mask) & (~page_mask); | 
 | 57 |  | 
 | 58 |     int64_t bootimg_size = V3_PAGE_SIZE + kernel_actual + ramdisk_actual; | 
 | 59 |     out->resize(bootimg_size); | 
 | 60 |  | 
 | 61 |     boot_img_hdr_v3* hdr = reinterpret_cast<boot_img_hdr_v3*>(out->data()); | 
 | 62 |  | 
 | 63 |     memcpy(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE); | 
 | 64 |     hdr->kernel_size = kernel.size(); | 
 | 65 |     hdr->ramdisk_size = ramdisk.size(); | 
 | 66 |     hdr->os_version = src.os_version; | 
 | 67 |     hdr->header_size = sizeof(boot_img_hdr_v3); | 
| Yi-Yo Chiang | c0c9e30 | 2021-03-23 17:48:23 +0800 | [diff] [blame] | 68 |     hdr->header_version = src.header_version; | 
 | 69 |  | 
 | 70 |     if (src.header_version >= 4) { | 
 | 71 |         auto hdr_v4 = reinterpret_cast<boot_img_hdr_v4*>(hdr); | 
 | 72 |         hdr_v4->signature_size = 0; | 
 | 73 |     } | 
| Steve Muckle | 15303f2 | 2020-03-18 14:55:06 -0700 | [diff] [blame] | 74 |  | 
 | 75 |     memcpy(hdr->magic + V3_PAGE_SIZE, kernel.data(), kernel.size()); | 
 | 76 |     memcpy(hdr->magic + V3_PAGE_SIZE + kernel_actual, ramdisk.data(), ramdisk.size()); | 
| Steve Muckle | 15303f2 | 2020-03-18 14:55:06 -0700 | [diff] [blame] | 77 | } | 
 | 78 |  | 
| Yi-Yo Chiang | ac4a136 | 2021-03-25 01:14:52 +0800 | [diff] [blame] | 79 | void mkbootimg(const std::vector<char>& kernel, const std::vector<char>& ramdisk, | 
 | 80 |                const std::vector<char>& second, const std::vector<char>& dtb, size_t base, | 
 | 81 |                const boot_img_hdr_v2& src, std::vector<char>* out) { | 
| Yi-Yo Chiang | c0c9e30 | 2021-03-23 17:48:23 +0800 | [diff] [blame] | 82 |     if (src.header_version >= 3) { | 
| Steve Muckle | 15303f2 | 2020-03-18 14:55:06 -0700 | [diff] [blame] | 83 |         if (!second.empty() || !dtb.empty()) { | 
| Yi-Yo Chiang | c0c9e30 | 2021-03-23 17:48:23 +0800 | [diff] [blame] | 84 |             die("Second stage bootloader and dtb not supported in v%d boot image\n", | 
 | 85 |                 src.header_version); | 
| Steve Muckle | 15303f2 | 2020-03-18 14:55:06 -0700 | [diff] [blame] | 86 |         } | 
| Yi-Yo Chiang | ac4a136 | 2021-03-25 01:14:52 +0800 | [diff] [blame] | 87 |         mkbootimg_v3_and_above(kernel, ramdisk, src, out); | 
 | 88 |         return; | 
| Steve Muckle | 15303f2 | 2020-03-18 14:55:06 -0700 | [diff] [blame] | 89 |     } | 
| Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 90 |     const size_t page_mask = src.page_size - 1; | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 91 |  | 
| Fernando Lugo | 91dfaec | 2018-04-11 19:02:59 -0700 | [diff] [blame] | 92 |     int64_t header_actual = (sizeof(boot_img_hdr_v1) + page_mask) & (~page_mask); | 
| Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 93 |     int64_t kernel_actual = (kernel.size() + page_mask) & (~page_mask); | 
 | 94 |     int64_t ramdisk_actual = (ramdisk.size() + page_mask) & (~page_mask); | 
 | 95 |     int64_t second_actual = (second.size() + page_mask) & (~page_mask); | 
| Hridya Valsaraju | 6c8fca6 | 2019-01-24 12:47:08 -0800 | [diff] [blame] | 96 |     int64_t dtb_actual = (dtb.size() + page_mask) & (~page_mask); | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 97 |  | 
| Hridya Valsaraju | 6c8fca6 | 2019-01-24 12:47:08 -0800 | [diff] [blame] | 98 |     int64_t bootimg_size = | 
 | 99 |             header_actual + kernel_actual + ramdisk_actual + second_actual + dtb_actual; | 
| Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 100 |     out->resize(bootimg_size); | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 101 |  | 
| Hridya Valsaraju | 6c8fca6 | 2019-01-24 12:47:08 -0800 | [diff] [blame] | 102 |     boot_img_hdr_v2* hdr = reinterpret_cast<boot_img_hdr_v2*>(out->data()); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 103 |  | 
| Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 104 |     *hdr = src; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 105 |     memcpy(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE); | 
| Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 106 |  | 
| Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 107 |     hdr->kernel_size = kernel.size(); | 
 | 108 |     hdr->ramdisk_size = ramdisk.size(); | 
 | 109 |     hdr->second_size = second.size(); | 
| JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 110 |  | 
| Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 111 |     hdr->kernel_addr += base; | 
 | 112 |     hdr->ramdisk_addr += base; | 
 | 113 |     hdr->second_addr += base; | 
 | 114 |     hdr->tags_addr += base; | 
| JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 115 |  | 
| Hridya Valsaraju | 6c8fca6 | 2019-01-24 12:47:08 -0800 | [diff] [blame] | 116 |     if (hdr->header_version == 1) { | 
| Hridya Valsaraju | 7d82413 | 2018-03-30 16:15:33 -0700 | [diff] [blame] | 117 |         hdr->header_size = sizeof(boot_img_hdr_v1); | 
| Hridya Valsaraju | 6c8fca6 | 2019-01-24 12:47:08 -0800 | [diff] [blame] | 118 |     } else if (hdr->header_version == 2) { | 
 | 119 |         hdr->header_size = sizeof(boot_img_hdr_v2); | 
 | 120 |         hdr->dtb_size = dtb.size(); | 
 | 121 |         hdr->dtb_addr += base; | 
| Hridya Valsaraju | 7d82413 | 2018-03-30 16:15:33 -0700 | [diff] [blame] | 122 |     } | 
 | 123 |  | 
| Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 124 |     memcpy(hdr->magic + hdr->page_size, kernel.data(), kernel.size()); | 
 | 125 |     memcpy(hdr->magic + hdr->page_size + kernel_actual, ramdisk.data(), ramdisk.size()); | 
 | 126 |     memcpy(hdr->magic + hdr->page_size + kernel_actual + ramdisk_actual, second.data(), | 
 | 127 |            second.size()); | 
| Hridya Valsaraju | 6c8fca6 | 2019-01-24 12:47:08 -0800 | [diff] [blame] | 128 |     memcpy(hdr->magic + hdr->page_size + kernel_actual + ramdisk_actual + second_actual, dtb.data(), | 
 | 129 |            dtb.size()); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 130 | } |