partitions.h revision e160f81713034e1b5cfa6f8d8b47870b47ae84c8
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// it should be passed in little endian order 42struct GPT_entry_raw { 43 uint8_t type_guid[16]; 44 uint8_t partition_guid[16]; 45 uint64_t first_lba; // little endian 46 uint64_t last_lba; 47 uint64_t flags; 48 uint16_t name[GPT_NAMELEN]; // UTF-16 LE 49}; 50 51struct GPT_mapping { 52 void *map_ptr; 53 void *ptr; 54 unsigned size; 55}; 56 57struct GPT_entry_table { 58 int fd; 59 60 struct GPT_mapping header_map; 61 struct GPT_mapping entries_map; 62 struct GPT_mapping sec_header_map; 63 struct GPT_mapping sec_entries_map; 64 65 struct GPT_header *header; 66 struct GPT_entry_raw *entries; 67 struct GPT_header *second_header; 68 struct GPT_entry_raw *second_entries; 69 70 unsigned sector_size; 71 unsigned partition_table_size; 72 int second_valid; 73}; 74 75struct GPT_header { 76 uint8_t signature[8]; 77 uint32_t revision; 78 uint32_t header_size; 79 uint32_t header_checksum; 80 uint32_t reserved_zeros; 81 uint64_t current_lba; 82 uint64_t backup_lba; 83 uint64_t first_usable_lba; 84 uint64_t last_usable_lba; 85 uint8_t disk_guid[16]; 86 uint64_t entries_lba; 87 uint32_t entries_count; 88 uint32_t entry_size; 89 uint32_t partition_array_checksum; 90 // the rest should be filled with zeros 91} __attribute__((packed)); 92 93struct GPT_content { 94 struct GPT_header header; 95 struct GPT_entry_raw *entries; 96}; 97 98 99struct GPT_entry_table* GPT_get_device(const char *, unsigned lba); 100 101void GPT_release_device(struct GPT_entry_table *); 102 103void GPT_edit_entry(struct GPT_entry_table *table, 104 struct GPT_entry_raw *old_entry, 105 struct GPT_entry_raw *new_entry); 106 107int GPT_delete_entry(struct GPT_entry_table *table, struct GPT_entry_raw *entry); 108 109void GPT_add_entry(struct GPT_entry_table *table, struct GPT_entry_raw *entry); 110 111struct GPT_entry_raw *GPT_get_pointer(struct GPT_entry_table *table, struct GPT_entry_raw *entry); 112struct GPT_entry_raw *GPT_get_pointer_by_guid(struct GPT_entry_table *, const char *); 113struct GPT_entry_raw *GPT_get_pointer_by_name(struct GPT_entry_table *, const char *); 114 115//Use after every edit operation 116void GPT_sync(); 117 118void GPT_to_UTF16(uint16_t *, const char *, int ); 119void GPT_from_UTF16(char *, const uint16_t *, int); 120 121int GPT_parse_entry(char *string, struct GPT_entry_raw *entry); 122 123void GPT_default_content(struct GPT_content *content, struct GPT_entry_table *table); 124 125void GPT_release_content(struct GPT_content *content); 126 127int GPT_parse_file(int fd, struct GPT_content *content); 128 129int GPT_write_content(const char *device, struct GPT_content *content); 130 131int gpt_mmap(struct GPT_mapping *mapping, uint64_t location, int size, int fd); 132 133void gpt_unmap(struct GPT_mapping *mapping); 134 135#endif 136