• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* libs/diskconfig/diskconfig.c
2  *
3  * Copyright 2008, The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #define LOG_TAG "config_mbr"
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdio.h>
23 
24 #include <cutils/log.h>
25 
26 #include <diskconfig/diskconfig.h>
27 
28 
29 /* start and len are in LBA units */
30 static void
cfg_pentry(struct pc_partition * pentry,uint8_t status,uint8_t type,uint32_t start,uint32_t len)31 cfg_pentry(struct pc_partition *pentry, uint8_t status, uint8_t type,
32            uint32_t start, uint32_t len)
33 {
34     if (len > 0) {
35         /* seems that somes BIOSens can get wedged on boot while verifying
36          * the mbr if these are 0 */
37         memset(&pentry->start, 0xff, sizeof(struct chs));
38         memset(&pentry->end, 0xff, sizeof(struct chs));
39     } else {
40         /* zero out the c/h/s entries.. they are not used */
41         memset(&pentry->start, 0, sizeof(struct chs));
42         memset(&pentry->end, 0, sizeof(struct chs));
43     }
44 
45     pentry->status = status;
46     pentry->type = type;
47     pentry->start_lba = start;
48     pentry->len_lba = len;
49 
50     ALOGI("Configuring pentry. status=0x%x type=0x%x start_lba=%u len_lba=%u",
51          pentry->status, pentry->type, pentry->start_lba, pentry->len_lba);
52 }
53 
54 
55 static inline uint32_t
kb_to_lba(uint32_t len_kb,uint32_t sect_size)56 kb_to_lba(uint32_t len_kb, uint32_t sect_size)
57 {
58     uint64_t lba;
59 
60     lba = (uint64_t)len_kb * 1024;
61     /* bump it up to the next LBA boundary just in case  */
62     lba = (lba + (uint64_t)sect_size - 1) & ~((uint64_t)sect_size - 1);
63     lba /= (uint64_t)sect_size;
64     if (lba >= 0xffffffffULL)
65         ALOGE("Error converting kb -> lba. 32bit overflow, expect weirdness");
66     return (uint32_t)(lba & 0xffffffffULL);
67 }
68 
69 
70 static struct write_list *
mk_pri_pentry(struct disk_info * dinfo,struct part_info * pinfo,int pnum,uint32_t * lba)71 mk_pri_pentry(struct disk_info *dinfo, struct part_info *pinfo, int pnum,
72               uint32_t *lba)
73 {
74     struct write_list *item;
75     struct pc_partition *pentry;
76 
77     if (pnum >= PC_NUM_BOOT_RECORD_PARTS) {
78         ALOGE("Maximum number of primary partition exceeded.");
79         return NULL;
80     }
81 
82     if (!(item = alloc_wl(sizeof(struct pc_partition)))) {
83         ALOGE("Unable to allocate memory for partition entry.");
84         return NULL;
85     }
86 
87     {
88         /* DO NOT DEREFERENCE */
89         struct pc_boot_record *mbr = (void *)PC_MBR_DISK_OFFSET;
90         /* grab the offset in mbr where to write this partition entry. */
91         item->offset = (loff_t)((uintptr_t)((uint8_t *)(&mbr->ptable[pnum])));
92     }
93 
94     pentry = (struct pc_partition *) &item->data;
95 
96     /* need a standard primary partition entry */
97     if (pinfo) {
98         /* need this to be 64 bit in case len_kb is large */
99         uint64_t len_lba;
100 
101         if (pinfo->len_kb != (uint32_t)-1) {
102             /* bump it up to the next LBA boundary just in case */
103             len_lba = ((uint64_t)pinfo->len_kb * 1024);
104             len_lba += ((uint64_t)dinfo->sect_size - 1);
105             len_lba &= ~((uint64_t)dinfo->sect_size - 1);
106             len_lba /= (uint64_t)dinfo->sect_size;
107         } else {
108             /* make it fill the rest of disk */
109             len_lba = dinfo->num_lba - *lba;
110         }
111 
112         cfg_pentry(pentry, ((pinfo->flags & PART_ACTIVE_FLAG) ?
113                             PC_PART_ACTIVE : PC_PART_NORMAL),
114                    pinfo->type, *lba, (uint32_t)len_lba);
115 
116         pinfo->start_lba = *lba;
117         *lba += (uint32_t)len_lba;
118     } else {
119         /* this should be made an extended partition, and should take
120          * up the rest of the disk as a primary partition */
121         cfg_pentry(pentry, PC_PART_NORMAL, PC_PART_TYPE_EXTENDED,
122                    *lba, dinfo->num_lba - *lba);
123 
124         /* note that we do not update the *lba because we now have to
125          * create a chain of extended partition tables, and first one is at
126          * *lba */
127     }
128 
129     return item;
130 }
131 
132 
133 /* This function configures an extended boot record at the beginning of an
134  * extended partition. This creates a logical partition and a pointer to
135  * the next EBR.
136  *
137  * ext_lba == The start of the toplevel extended partition (pointed to by the
138  * entry in the MBR).
139  */
140 static struct write_list *
mk_ext_pentry(struct disk_info * dinfo,struct part_info * pinfo,uint32_t * lba,uint32_t ext_lba,struct part_info * pnext)141 mk_ext_pentry(struct disk_info *dinfo, struct part_info *pinfo, uint32_t *lba,
142               uint32_t ext_lba, struct part_info *pnext)
143 {
144     struct write_list *item;
145     struct pc_boot_record *ebr;
146     uint32_t len; /* in lba units */
147 
148     if (!(item = alloc_wl(sizeof(struct pc_boot_record)))) {
149         ALOGE("Unable to allocate memory for EBR.");
150         return NULL;
151     }
152 
153     /* we are going to write the ebr at the current LBA, and then bump the
154      * lba counter since that is where the logical data partition will start */
155     item->offset = ((loff_t)(*lba)) * dinfo->sect_size;
156     (*lba)++;
157 
158     ebr = (struct pc_boot_record *) &item->data;
159     memset(ebr, 0, sizeof(struct pc_boot_record));
160     ebr->mbr_sig = PC_BIOS_BOOT_SIG;
161 
162     if (pinfo->len_kb != (uint32_t)-1)
163         len = kb_to_lba(pinfo->len_kb, dinfo->sect_size);
164     else {
165         if (pnext) {
166             ALOGE("Only the last partition can be specified to fill the disk "
167                  "(name = '%s')", pinfo->name);
168             goto fail;
169         }
170         len = dinfo->num_lba - *lba;
171         /* update the pinfo structure to reflect the new size, for
172          * bookkeeping */
173         pinfo->len_kb =
174             (uint32_t)(((uint64_t)len * (uint64_t)dinfo->sect_size) /
175                        ((uint64_t)1024));
176     }
177 
178     cfg_pentry(&ebr->ptable[PC_EBR_LOGICAL_PART], PC_PART_NORMAL,
179                pinfo->type, 1, len);
180 
181     pinfo->start_lba = *lba;
182     *lba += len;
183 
184     /* If this is not the last partition, we have to create a link to the
185      * next extended partition.
186      *
187      * Otherwise, there's nothing to do since the "pointer entry" is
188      * already zero-filled.
189      */
190     if (pnext) {
191         /* The start lba for next partition is an offset from the beginning
192          * of the top-level extended partition */
193         uint32_t next_start_lba = *lba - ext_lba;
194         uint32_t next_len_lba;
195         if (pnext->len_kb != (uint32_t)-1)
196             next_len_lba = 1 + kb_to_lba(pnext->len_kb, dinfo->sect_size);
197         else
198             next_len_lba = dinfo->num_lba - *lba;
199         cfg_pentry(&ebr->ptable[PC_EBR_NEXT_PTR_PART], PC_PART_NORMAL,
200                    PC_PART_TYPE_EXTENDED, next_start_lba, next_len_lba);
201     }
202 
203     return item;
204 
205 fail:
206     free_wl(item);
207     return NULL;
208 }
209 
210 
211 static struct write_list *
mk_mbr_sig()212 mk_mbr_sig()
213 {
214     struct write_list *item;
215     if (!(item = alloc_wl(sizeof(uint16_t)))) {
216         ALOGE("Unable to allocate memory for MBR signature.");
217         return NULL;
218     }
219 
220     {
221         /* DO NOT DEREFERENCE */
222         struct pc_boot_record *mbr = (void *)PC_MBR_DISK_OFFSET;
223         /* grab the offset in mbr where to write mbr signature. */
224         item->offset = (loff_t)((uintptr_t)((uint8_t *)(&mbr->mbr_sig)));
225     }
226 
227     *((uint16_t*)item->data) = PC_BIOS_BOOT_SIG;
228     return item;
229 }
230 
231 struct write_list *
config_mbr(struct disk_info * dinfo)232 config_mbr(struct disk_info *dinfo)
233 {
234     struct part_info *pinfo;
235     uint32_t cur_lba = dinfo->skip_lba;
236     uint32_t ext_lba = 0;
237     struct write_list *wr_list = NULL;
238     struct write_list *temp_wr = NULL;
239     int cnt = 0;
240     int extended = 0;
241 
242     if (!dinfo->part_lst)
243         return NULL;
244 
245     for (cnt = 0; cnt < dinfo->num_parts; ++cnt) {
246         pinfo = &dinfo->part_lst[cnt];
247 
248         /* Should we create an extedned partition? */
249         if (cnt == (PC_NUM_BOOT_RECORD_PARTS - 1)) {
250             if (cnt + 1 < dinfo->num_parts) {
251                 extended = 1;
252                 ext_lba = cur_lba;
253                 if ((temp_wr = mk_pri_pentry(dinfo, NULL, cnt, &cur_lba)))
254                     wlist_add(&wr_list, temp_wr);
255                 else {
256                     ALOGE("Cannot create primary extended partition.");
257                     goto fail;
258                 }
259             }
260         }
261 
262         /* if extended, need 1 lba for ebr */
263         if ((cur_lba + extended) >= dinfo->num_lba)
264             goto nospace;
265         else if (pinfo->len_kb != (uint32_t)-1) {
266             uint32_t sz_lba = (pinfo->len_kb / dinfo->sect_size) * 1024;
267             if ((cur_lba + sz_lba + extended) > dinfo->num_lba)
268                 goto nospace;
269         }
270 
271         if (!extended)
272             temp_wr = mk_pri_pentry(dinfo, pinfo, cnt, &cur_lba);
273         else {
274             struct part_info *pnext;
275             pnext = cnt + 1 < dinfo->num_parts ? &dinfo->part_lst[cnt+1] : NULL;
276             temp_wr = mk_ext_pentry(dinfo, pinfo, &cur_lba, ext_lba, pnext);
277         }
278 
279         if (temp_wr)
280             wlist_add(&wr_list, temp_wr);
281         else {
282             ALOGE("Cannot create partition %d (%s).", cnt, pinfo->name);
283             goto fail;
284         }
285     }
286 
287     /* fill in the rest of the MBR with empty parts (if needed). */
288     for (; cnt < PC_NUM_BOOT_RECORD_PARTS; ++cnt) {
289         struct part_info blank;
290         cur_lba = 0;
291         memset(&blank, 0, sizeof(struct part_info));
292         if (!(temp_wr = mk_pri_pentry(dinfo, &blank, cnt, &cur_lba))) {
293             ALOGE("Cannot create blank partition %d.", cnt);
294             goto fail;
295         }
296         wlist_add(&wr_list, temp_wr);
297     }
298 
299     if ((temp_wr = mk_mbr_sig()))
300         wlist_add(&wr_list, temp_wr);
301     else {
302         ALOGE("Cannot set MBR signature");
303         goto fail;
304     }
305 
306     return wr_list;
307 
308 nospace:
309     ALOGE("Not enough space to add parttion '%s'.", pinfo->name);
310 
311 fail:
312     wlist_free(wr_list);
313     return NULL;
314 }
315 
316 
317 /* Returns the device path of the partition referred to by 'name'
318  * Must be freed by the caller.
319  */
320 char *
find_mbr_part(struct disk_info * dinfo,const char * name)321 find_mbr_part(struct disk_info *dinfo, const char *name)
322 {
323     struct part_info *plist = dinfo->part_lst;
324     int num = 0;
325     char *dev_name = NULL;
326     int has_extended = (dinfo->num_parts > PC_NUM_BOOT_RECORD_PARTS);
327 
328     for(num = 1; num <= dinfo->num_parts; ++num) {
329         if (!strcmp(plist[num-1].name, name))
330             break;
331     }
332 
333     if (num > dinfo->num_parts)
334         return NULL;
335 
336     if (has_extended && (num >= PC_NUM_BOOT_RECORD_PARTS))
337         num++;
338 
339     if (!(dev_name = malloc(MAX_NAME_LEN))) {
340         ALOGE("Cannot allocate memory.");
341         return NULL;
342     }
343 
344     num = snprintf(dev_name, MAX_NAME_LEN, "%s%d", dinfo->device, num);
345     if (num >= MAX_NAME_LEN) {
346         ALOGE("Device name is too long?!");
347         free(dev_name);
348         return NULL;
349     }
350 
351     return dev_name;
352 }
353