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