Jiyong Park | d352412 | 2022-07-07 15:00:51 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | // This program loads kernel and initrd which the system will boot into when |
| 18 | // panic occurs. |
| 19 | |
| 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <linux/kexec.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/syscall.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <unistd.h> |
| 29 | |
Jiyong Park | 8d2a707 | 2022-07-15 14:34:27 +0900 | [diff] [blame] | 30 | #if defined(__aarch64__) |
| 31 | #define EARLYCON "earlycon=uart8250,mmio,0x3f8" |
| 32 | #elif defined(__x86_64__) |
| 33 | #define EARLYCON "earlycon=uart8250,io,0x3f8" |
| 34 | #endif |
| 35 | |
| 36 | static const char *KERNEL = "/system/etc/microdroid_crashdump_kernel"; |
| 37 | static const char *INITRD = "/system/etc/microdroid_crashdump_initrd.img"; |
| 38 | static const char *CMDLINE = "1 panic=-1 rdinit=/bin/crashdump nr_cpus=1 reset_devices " |
| 39 | "console=hvc0 " EARLYCON; |
| 40 | |
Jiyong Park | d352412 | 2022-07-07 15:00:51 +0900 | [diff] [blame] | 41 | static int open_checked(const char* path) { |
| 42 | int fd = open(path, O_RDONLY); |
| 43 | if (fd == -1) { |
| 44 | fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno)); |
| 45 | exit(1); |
| 46 | } |
| 47 | return fd; |
| 48 | } |
| 49 | |
Jiyong Park | 8d2a707 | 2022-07-15 14:34:27 +0900 | [diff] [blame] | 50 | int main() { |
| 51 | unsigned long cmdline_len = strlen(CMDLINE) + 1; // include null terminator, otherwise EINVAL |
Jiyong Park | d352412 | 2022-07-07 15:00:51 +0900 | [diff] [blame] | 52 | |
Jiyong Park | 8d2a707 | 2022-07-15 14:34:27 +0900 | [diff] [blame] | 53 | if (syscall(SYS_kexec_file_load, open_checked(KERNEL), open_checked(INITRD), cmdline_len, |
| 54 | CMDLINE, KEXEC_FILE_ON_CRASH) == -1) { |
Jiyong Park | d352412 | 2022-07-07 15:00:51 +0900 | [diff] [blame] | 55 | fprintf(stderr, "Failed to load panic kernel: %s\n", strerror(errno)); |
| 56 | return 1; |
| 57 | } |
| 58 | return 0; |
| 59 | } |