David Zeuthen | aaa6282 | 2015-09-01 14:58:36 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
| 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <linux/fs.h> |
| 21 | #include <sys/ioctl.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include <arpa/inet.h> |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | |
Tao Bao | 129b4bc | 2018-03-31 10:07:52 -0700 | [diff] [blame] | 31 | #include <bootloader_message/bootloader_message.h> |
David Zeuthen | aaa6282 | 2015-09-01 14:58:36 -0400 | [diff] [blame] | 32 | #include <cutils/properties.h> |
David Zeuthen | aaa6282 | 2015-09-01 14:58:36 -0400 | [diff] [blame] | 33 | #include <fs_mgr.h> |
| 34 | |
| 35 | #include "bootinfo.h" |
| 36 | |
| 37 | // Open the appropriate fstab file and fallback to /fstab.device if |
| 38 | // that's what's being used. |
| 39 | static struct fstab *open_fstab(void) |
| 40 | { |
Bowgo Tsai | 78c7469 | 2017-03-10 19:20:54 +0800 | [diff] [blame] | 41 | struct fstab *fstab = fs_mgr_read_fstab_default(); |
David Zeuthen | aaa6282 | 2015-09-01 14:58:36 -0400 | [diff] [blame] | 42 | if (fstab != NULL) |
| 43 | return fstab; |
| 44 | |
Bowgo Tsai | 732b209 | 2017-03-29 16:07:12 +0800 | [diff] [blame] | 45 | fstab = fs_mgr_read_fstab("/fstab.device"); |
David Zeuthen | aaa6282 | 2015-09-01 14:58:36 -0400 | [diff] [blame] | 46 | return fstab; |
| 47 | } |
| 48 | |
| 49 | int boot_info_open_partition(const char *name, uint64_t *out_size, int flags) |
| 50 | { |
| 51 | char *path; |
| 52 | int fd; |
| 53 | struct fstab *fstab; |
| 54 | struct fstab_rec *record; |
| 55 | |
| 56 | // We can't use fs_mgr to look up |name| because fstab doesn't list |
| 57 | // every slot partition (it uses the slotselect option to mask the |
| 58 | // suffix) and |slot| is expected to be of that form, e.g. boot_a. |
| 59 | // |
| 60 | // We can however assume that there's an entry for the /misc mount |
| 61 | // point and use that to get the device file for the misc |
| 62 | // partition. From there we'll assume that a by-name scheme is used |
| 63 | // so we can just replace the trailing "misc" by the given |name|, |
| 64 | // e.g. |
| 65 | // |
| 66 | // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc -> |
| 67 | // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a |
| 68 | // |
| 69 | // If needed, it's possible to relax this assumption in the future |
| 70 | // by trawling /sys/block looking for the appropriate sibling of |
| 71 | // misc and then finding an entry in /dev matching the sysfs entry. |
| 72 | |
| 73 | fstab = open_fstab(); |
| 74 | if (fstab == NULL) |
| 75 | return -1; |
| 76 | record = fs_mgr_get_entry_for_mount_point(fstab, "/misc"); |
| 77 | if (record == NULL) { |
| 78 | fs_mgr_free_fstab(fstab); |
| 79 | return -1; |
| 80 | } |
| 81 | if (strcmp(name, "misc") == 0) { |
| 82 | path = strdup(record->blk_device); |
| 83 | } else { |
| 84 | size_t trimmed_len, name_len; |
| 85 | const char *end_slash = strrchr(record->blk_device, '/'); |
| 86 | if (end_slash == NULL) { |
| 87 | fs_mgr_free_fstab(fstab); |
| 88 | return -1; |
| 89 | } |
| 90 | trimmed_len = end_slash - record->blk_device + 1; |
| 91 | name_len = strlen(name); |
Bowgo Tsai | 4b7ef27 | 2017-09-28 15:18:37 +0800 | [diff] [blame] | 92 | path = static_cast<char *>(calloc(trimmed_len + name_len + 1, 1)); |
David Zeuthen | aaa6282 | 2015-09-01 14:58:36 -0400 | [diff] [blame] | 93 | strncpy(path, record->blk_device, trimmed_len); |
| 94 | strncpy(path + trimmed_len, name, name_len); |
| 95 | } |
| 96 | fs_mgr_free_fstab(fstab); |
| 97 | |
| 98 | fd = open(path, flags); |
| 99 | free(path); |
| 100 | |
| 101 | // If we successfully opened the device, get size if requested. |
| 102 | if (fd != -1 && out_size != NULL) { |
| 103 | if (ioctl(fd, BLKGETSIZE64, out_size) != 0) { |
| 104 | close(fd); |
| 105 | return -1; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return fd; |
| 110 | } |
| 111 | |
Tao Bao | 4fa7420 | 2016-06-09 22:40:05 -0700 | [diff] [blame] | 112 | // As per struct bootloader_message_ab which is defined in |
David Zeuthen | aaa6282 | 2015-09-01 14:58:36 -0400 | [diff] [blame] | 113 | // bootable/recovery/bootloader.h we can use the 32 bytes in the |
| 114 | // bootctrl_suffix field provided that they start with the active slot |
| 115 | // suffix terminated by NUL. It just so happens that BrilloBootInfo is |
| 116 | // laid out this way. |
Tao Bao | 4fa7420 | 2016-06-09 22:40:05 -0700 | [diff] [blame] | 117 | #define BOOTINFO_OFFSET offsetof(struct bootloader_message_ab, slot_suffix) |
David Zeuthen | aaa6282 | 2015-09-01 14:58:36 -0400 | [diff] [blame] | 118 | |
| 119 | bool boot_info_load(BrilloBootInfo *out_info) |
| 120 | { |
| 121 | int fd; |
| 122 | |
| 123 | memset(out_info, '\0', sizeof(BrilloBootInfo)); |
| 124 | |
| 125 | fd = boot_info_open_partition("misc", NULL, O_RDONLY); |
| 126 | if (fd == -1) |
| 127 | return false; |
| 128 | if (lseek(fd, BOOTINFO_OFFSET, SEEK_SET) != BOOTINFO_OFFSET) { |
| 129 | close(fd); |
| 130 | return false; |
| 131 | } |
| 132 | ssize_t num_read; |
| 133 | do { |
| 134 | num_read = read(fd, (void*) out_info, sizeof(BrilloBootInfo)); |
| 135 | } while (num_read == -1 && errno == EINTR); |
| 136 | close(fd); |
| 137 | if (num_read != sizeof(BrilloBootInfo)) |
| 138 | return false; |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | bool boot_info_save(BrilloBootInfo *info) |
| 143 | { |
| 144 | int fd; |
| 145 | |
| 146 | fd = boot_info_open_partition("misc", NULL, O_RDWR); |
| 147 | if (fd == -1) |
| 148 | return false; |
| 149 | if (lseek(fd, BOOTINFO_OFFSET, SEEK_SET) != BOOTINFO_OFFSET) { |
| 150 | close(fd); |
| 151 | return false; |
| 152 | } |
| 153 | ssize_t num_written; |
| 154 | do { |
| 155 | num_written = write(fd, (void*) info, sizeof(BrilloBootInfo)); |
| 156 | } while (num_written == -1 && errno == EINTR); |
| 157 | close(fd); |
| 158 | if (num_written != sizeof(BrilloBootInfo)) |
| 159 | return false; |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | bool boot_info_validate(BrilloBootInfo* info) |
| 164 | { |
| 165 | if (info->magic[0] != 'B' || |
| 166 | info->magic[1] != 'C' || |
| 167 | info->magic[2] != 'c') |
| 168 | return false; |
| 169 | if (info->active_slot >= 2) |
| 170 | return false; |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | void boot_info_reset(BrilloBootInfo* info) |
| 175 | { |
David Zeuthen | aaa6282 | 2015-09-01 14:58:36 -0400 | [diff] [blame] | 176 | memset(info, '\0', sizeof(BrilloBootInfo)); |
| 177 | info->magic[0] = 'B'; |
| 178 | info->magic[1] = 'C'; |
| 179 | info->magic[2] = 'c'; |
| 180 | } |