San Mehat | a6391f1 | 2010-03-10 12:46:00 -0800 | [diff] [blame] | 1 | /* system/core/include/diskconfig/diskconfig.h |
| 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 | #ifndef __LIBS_DISKCONFIG_H |
| 19 | #define __LIBS_DISKCONFIG_H |
| 20 | |
| 21 | #include <stdint.h> |
| 22 | |
| 23 | #ifdef __cplusplus |
| 24 | extern "C" { |
| 25 | #endif |
| 26 | |
| 27 | #define MAX_NAME_LEN 512 |
| 28 | #define MAX_NUM_PARTS 16 |
| 29 | |
| 30 | /* known partition schemes */ |
| 31 | #define PART_SCHEME_MBR 0x1 |
| 32 | #define PART_SCHEME_GPT 0x2 |
| 33 | |
| 34 | /* PC Bios partition status */ |
| 35 | #define PC_PART_ACTIVE 0x80 |
| 36 | #define PC_PART_NORMAL 0x0 |
| 37 | |
| 38 | /* Known (rather, used by us) partition types */ |
| 39 | #define PC_PART_TYPE_LINUX 0x83 |
| 40 | #define PC_PART_TYPE_EXTENDED 0x05 |
| 41 | #define PC_PART_TYPE_FAT32 0x0c |
| 42 | |
| 43 | #define PC_NUM_BOOT_RECORD_PARTS 4 |
| 44 | |
| 45 | #define PC_EBR_LOGICAL_PART 0 |
| 46 | #define PC_EBR_NEXT_PTR_PART 1 |
| 47 | |
| 48 | #define PC_BIOS_BOOT_SIG 0xAA55 |
| 49 | |
| 50 | #define PC_MBR_DISK_OFFSET 0 |
| 51 | #define PC_MBR_SIZE 512 |
| 52 | |
| 53 | #define PART_ACTIVE_FLAG 0x1 |
| 54 | |
| 55 | struct chs { |
| 56 | uint8_t head; |
| 57 | uint8_t sector; |
| 58 | uint8_t cylinder; |
| 59 | } __attribute__((__packed__)); |
| 60 | |
| 61 | /* 16 byte pc partition descriptor that sits in MBR and EPBR. |
| 62 | * Note: multi-byte entities have little-endian layout on disk */ |
| 63 | struct pc_partition { |
| 64 | uint8_t status; /* byte 0 */ |
| 65 | struct chs start; /* bytes 1-3 */ |
| 66 | uint8_t type; /* byte 4 */ |
| 67 | struct chs end; /* bytes 5-7 */ |
| 68 | uint32_t start_lba; /* bytes 8-11 */ |
| 69 | uint32_t len_lba; /* bytes 12-15 */ |
| 70 | } __attribute__((__packed__)); |
| 71 | |
| 72 | struct pc_boot_record { |
| 73 | uint8_t code[440]; /* bytes 0-439 */ |
| 74 | uint32_t disk_sig; /* bytes 440-443 */ |
| 75 | uint16_t pad; /* bytes 444-445 */ |
| 76 | struct pc_partition ptable[PC_NUM_BOOT_RECORD_PARTS]; /* bytes 446-509 */ |
| 77 | uint16_t mbr_sig; /* bytes 510-511 */ |
| 78 | } __attribute__((__packed__)); |
| 79 | |
| 80 | struct part_info { |
| 81 | char *name; |
| 82 | uint8_t flags; |
| 83 | uint8_t type; |
| 84 | uint32_t len_kb; /* in 1K-bytes */ |
| 85 | uint32_t start_lba; /* the LBA where this partition begins */ |
| 86 | }; |
| 87 | |
| 88 | struct disk_info { |
| 89 | char *device; |
| 90 | uint8_t scheme; |
| 91 | int sect_size; /* expected sector size in bytes. MUST BE POWER OF 2 */ |
| 92 | uint32_t skip_lba; /* in sectors (1 unit of LBA) */ |
| 93 | uint32_t num_lba; /* the size of the disk in LBA units */ |
| 94 | struct part_info *part_lst; |
| 95 | int num_parts; |
| 96 | }; |
| 97 | |
| 98 | struct write_list { |
| 99 | struct write_list *next; |
| 100 | loff_t offset; |
| 101 | uint32_t len; |
| 102 | uint8_t data[0]; |
| 103 | }; |
| 104 | |
| 105 | |
| 106 | struct write_list *alloc_wl(uint32_t data_len); |
| 107 | void free_wl(struct write_list *item); |
| 108 | struct write_list *wlist_add(struct write_list **lst, struct write_list *item); |
| 109 | void wlist_free(struct write_list *lst); |
| 110 | int wlist_commit(int fd, struct write_list *lst, int test); |
| 111 | |
| 112 | struct disk_info *load_diskconfig(const char *fn, char *path_override); |
| 113 | int dump_disk_config(struct disk_info *dinfo); |
| 114 | int apply_disk_config(struct disk_info *dinfo, int test); |
| 115 | char *find_part_device(struct disk_info *dinfo, const char *name); |
| 116 | int process_disk_config(struct disk_info *dinfo); |
| 117 | struct part_info *find_part(struct disk_info *dinfo, const char *name); |
| 118 | |
| 119 | int write_raw_image(const char *dst, const char *src, loff_t offset, int test); |
| 120 | |
| 121 | /* For MBR partition schemes */ |
| 122 | struct write_list *config_mbr(struct disk_info *dinfo); |
| 123 | char *find_mbr_part(struct disk_info *dinfo, const char *name); |
| 124 | |
| 125 | #ifdef __cplusplus |
| 126 | } |
| 127 | #endif |
| 128 | |
| 129 | #endif /* __LIBS_DISKCONFIG_H */ |