• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2009-2013, Google Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *  * Neither the name of Google, Inc. nor the names of its contributors
15  *    may be used to endorse or promote products derived from this
16  *    software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 
33 #ifndef __FASTBOOTD_PATITIONS_
34 #define __FASTBOOTD_PATITIONS_
35 
36 #include <stdint.h>
37 
38 #define GPT_ENTRIES 128
39 #define GPT_NAMELEN 36
40 
41 #define GPT_FLAG_SYSTEM (1ULL << 0)
42 #define GPT_FLAG_BOOTABLE (1ULL << 2)
43 #define GPT_FLAG_READONLY (1ULL << 60)
44 #define GPT_FLAG_DOAUTOMOUNT (1ULL << 63)
45 
46 // it should be passed in little endian order
47 struct GPT_entry_raw {
48     uint8_t type_guid[16];
49     uint8_t partition_guid[16];
50     uint64_t first_lba; // little endian
51     uint64_t last_lba;
52     uint64_t flags;
53     uint16_t name[GPT_NAMELEN]; // UTF-16 LE
54 };
55 
56 struct GPT_mapping {
57     void *map_ptr;
58     void *ptr;
59     unsigned size;
60 };
61 
62 struct GPT_entry_table {
63     int fd;
64 
65     struct GPT_mapping header_map;
66     struct GPT_mapping entries_map;
67     struct GPT_mapping sec_header_map;
68     struct GPT_mapping sec_entries_map;
69 
70     struct GPT_header *header;
71     struct GPT_entry_raw *entries;
72     struct GPT_header *second_header;
73     struct GPT_entry_raw *second_entries;
74 
75     unsigned sector_size;
76     unsigned partition_table_size;
77     int second_valid;
78 };
79 
80 struct GPT_header {
81     uint8_t signature[8];
82     uint32_t revision;
83     uint32_t header_size;
84     uint32_t header_checksum;
85     uint32_t reserved_zeros;
86     uint64_t current_lba;
87     uint64_t backup_lba;
88     uint64_t first_usable_lba;
89     uint64_t last_usable_lba;
90     uint8_t disk_guid[16];
91     uint64_t entries_lba;
92     uint32_t entries_count;
93     uint32_t entry_size;
94     uint32_t partition_array_checksum;
95     // the rest should be filled with zeros
96 } __attribute__((packed));
97 
98 struct GPT_content {
99     struct GPT_header header;
100     struct GPT_entry_raw *entries;
101 };
102 
103 
104 struct GPT_entry_table* GPT_get_device(const char *, unsigned lba);
105 
106 void GPT_release_device(struct GPT_entry_table *);
107 
108 void GPT_edit_entry(struct GPT_entry_table *table,
109                     struct GPT_entry_raw *old_entry,
110                     struct GPT_entry_raw *new_entry);
111 
112 int GPT_delete_entry(struct GPT_entry_table *table, struct GPT_entry_raw *entry);
113 
114 void GPT_add_entry(struct GPT_entry_table *table, struct GPT_entry_raw *entry);
115 
116 struct GPT_entry_raw *GPT_get_pointer(struct GPT_entry_table *table, struct GPT_entry_raw *entry);
117 struct GPT_entry_raw *GPT_get_pointer_by_guid(struct GPT_entry_table *, const char *);
118 struct GPT_entry_raw *GPT_get_pointer_by_name(struct GPT_entry_table *, const char *);
119 
120 //Use after every edit operation
121 void GPT_sync();
122 
123 void GPT_to_UTF16(uint16_t *, const char *, int );
124 void GPT_from_UTF16(char *, const uint16_t *, int);
125 
126 int GPT_parse_entry(char *string, struct GPT_entry_raw *entry);
127 
128 void GPT_default_content(struct GPT_content *content, struct GPT_entry_table *table);
129 
130 void GPT_release_content(struct GPT_content *content);
131 
132 int GPT_parse_file(int fd, struct GPT_content *content);
133 
134 int GPT_write_content(const char *device, struct GPT_content *content);
135 
136 int gpt_mmap(struct GPT_mapping *mapping, uint64_t location, int size, int fd);
137 
138 void gpt_unmap(struct GPT_mapping *mapping);
139 
140 #endif
141