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