blob: 2e8c334dc7b74d5b286feb393a5467b181ccac76 [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
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -080031#include "fastboot.h"
32
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36
Hridya Valsaraju88de5552018-03-30 16:15:33 -070037void bootimg_set_cmdline(boot_img_hdr_v1* h, const char* cmdline) {
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -080038 if (strlen(cmdline) >= sizeof(h->cmdline)) die("command line too large: %zu", strlen(cmdline));
39 strcpy(reinterpret_cast<char*>(h->cmdline), cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040}
41
Hridya Valsaraju88de5552018-03-30 16:15:33 -070042boot_img_hdr_v1* mkbootimg(void* kernel, int64_t kernel_size, off_t kernel_offset, void* ramdisk,
43 int64_t ramdisk_size, off_t ramdisk_offset, void* second,
44 int64_t second_size, off_t second_offset, size_t page_size, size_t base,
45 off_t tags_offset, uint32_t header_version, int64_t* bootimg_size) {
Elliott Hughesfc797672015-04-07 20:12:50 -070046 size_t page_mask = page_size - 1;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -080047
Fernando Lugoc43dd752018-04-11 19:02:59 -070048 int64_t header_actual = (sizeof(boot_img_hdr_v1) + page_mask) & (~page_mask);
Elliott Hughesfc797672015-04-07 20:12:50 -070049 int64_t kernel_actual = (kernel_size + page_mask) & (~page_mask);
50 int64_t ramdisk_actual = (ramdisk_size + page_mask) & (~page_mask);
51 int64_t second_actual = (second_size + page_mask) & (~page_mask);
Anatol Pomazau5ae3f932012-02-28 07:21:08 -080052
Hridya Valsaraju88de5552018-03-30 16:15:33 -070053 *bootimg_size = header_actual + kernel_actual + ramdisk_actual + second_actual;
Anatol Pomazau5ae3f932012-02-28 07:21:08 -080054
Hridya Valsaraju88de5552018-03-30 16:15:33 -070055 boot_img_hdr_v1* hdr = reinterpret_cast<boot_img_hdr_v1*>(calloc(*bootimg_size, 1));
Elliott Hughesfc797672015-04-07 20:12:50 -070056 if (hdr == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057 return hdr;
58 }
59
60 memcpy(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE);
Anatol Pomazau5ae3f932012-02-28 07:21:08 -080061
Brian Swetland2a63bb72009-04-28 16:05:07 -070062 hdr->kernel_size = kernel_size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063 hdr->ramdisk_size = ramdisk_size;
Brian Swetland2a63bb72009-04-28 16:05:07 -070064 hdr->second_size = second_size;
JP Abgrall7b8970c2013-03-07 17:06:41 -080065
66 hdr->kernel_addr = base + kernel_offset;
67 hdr->ramdisk_addr = base + ramdisk_offset;
68 hdr->second_addr = base + second_offset;
69 hdr->tags_addr = base + tags_offset;
70
Brian Swetland2a63bb72009-04-28 16:05:07 -070071 hdr->page_size = page_size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072
Hridya Valsaraju88de5552018-03-30 16:15:33 -070073 if (header_version) {
74 hdr->header_version = header_version;
75 hdr->header_size = sizeof(boot_img_hdr_v1);
76 }
77
Elliott Hughesfc797672015-04-07 20:12:50 -070078 memcpy(hdr->magic + page_size, kernel, kernel_size);
79 memcpy(hdr->magic + page_size + kernel_actual, ramdisk, ramdisk_size);
80 memcpy(hdr->magic + page_size + kernel_actual + ramdisk_actual, second, second_size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081 return hdr;
82}