blob: 46d4bd30e12cfab5d5ae4a6723e3bc4a955238b3 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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 Pomazau5ae3f932012-02-28 07:21:08 -080012 * the documentation and/or other materials provided with the
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080013 * 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 Pomazau5ae3f932012-02-28 07:21:08 -080022 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023 * 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 Hughes253c18d2015-03-18 22:47:09 -070029#include "bootimg_utils.h"
30
Aaron Wisnerdb511202018-06-26 15:38:35 -050031#include "util.h"
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -080032
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36
Hridya Valsaraju6c8fca62019-01-24 12:47:08 -080037void bootimg_set_cmdline(boot_img_hdr_v2* h, const std::string& cmdline) {
Elliott Hughes577e8b42018-04-05 16:12:47 -070038 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 Projectdd7bc332009-03-03 19:32:55 -080040}
41
Hridya Valsaraju6c8fca62019-01-24 12:47:08 -080042boot_img_hdr_v2* mkbootimg(const std::vector<char>& kernel, const std::vector<char>& ramdisk,
43 const std::vector<char>& second, const std::vector<char>& dtb,
44 size_t base, const boot_img_hdr_v2& src, std::vector<char>* out) {
Elliott Hughes577e8b42018-04-05 16:12:47 -070045 const size_t page_mask = src.page_size - 1;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -080046
Fernando Lugo91dfaec2018-04-11 19:02:59 -070047 int64_t header_actual = (sizeof(boot_img_hdr_v1) + page_mask) & (~page_mask);
Tom Cherrydfd85df2018-09-20 14:45:05 -070048 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);
Hridya Valsaraju6c8fca62019-01-24 12:47:08 -080051 int64_t dtb_actual = (dtb.size() + page_mask) & (~page_mask);
Anatol Pomazau5ae3f932012-02-28 07:21:08 -080052
Hridya Valsaraju6c8fca62019-01-24 12:47:08 -080053 int64_t bootimg_size =
54 header_actual + kernel_actual + ramdisk_actual + second_actual + dtb_actual;
Tom Cherrydfd85df2018-09-20 14:45:05 -070055 out->resize(bootimg_size);
Anatol Pomazau5ae3f932012-02-28 07:21:08 -080056
Hridya Valsaraju6c8fca62019-01-24 12:47:08 -080057 boot_img_hdr_v2* hdr = reinterpret_cast<boot_img_hdr_v2*>(out->data());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058
Elliott Hughes577e8b42018-04-05 16:12:47 -070059 *hdr = src;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060 memcpy(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE);
Anatol Pomazau5ae3f932012-02-28 07:21:08 -080061
Tom Cherrydfd85df2018-09-20 14:45:05 -070062 hdr->kernel_size = kernel.size();
63 hdr->ramdisk_size = ramdisk.size();
64 hdr->second_size = second.size();
JP Abgrall7b8970c2013-03-07 17:06:41 -080065
Elliott Hughes577e8b42018-04-05 16:12:47 -070066 hdr->kernel_addr += base;
67 hdr->ramdisk_addr += base;
68 hdr->second_addr += base;
69 hdr->tags_addr += base;
JP Abgrall7b8970c2013-03-07 17:06:41 -080070
Hridya Valsaraju6c8fca62019-01-24 12:47:08 -080071 if (hdr->header_version == 1) {
Hridya Valsaraju7d824132018-03-30 16:15:33 -070072 hdr->header_size = sizeof(boot_img_hdr_v1);
Hridya Valsaraju6c8fca62019-01-24 12:47:08 -080073 } else if (hdr->header_version == 2) {
74 hdr->header_size = sizeof(boot_img_hdr_v2);
75 hdr->dtb_size = dtb.size();
76 hdr->dtb_addr += base;
Hridya Valsaraju7d824132018-03-30 16:15:33 -070077 }
78
Tom Cherrydfd85df2018-09-20 14:45:05 -070079 memcpy(hdr->magic + hdr->page_size, kernel.data(), kernel.size());
80 memcpy(hdr->magic + hdr->page_size + kernel_actual, ramdisk.data(), ramdisk.size());
81 memcpy(hdr->magic + hdr->page_size + kernel_actual + ramdisk_actual, second.data(),
82 second.size());
Hridya Valsaraju6c8fca62019-01-24 12:47:08 -080083 memcpy(hdr->magic + hdr->page_size + kernel_actual + ramdisk_actual + second_actual, dtb.data(),
84 dtb.size());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085 return hdr;
86}