OLD | NEW |
(Empty) | |
| 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. |
| 4 * |
| 5 * Header file for cgpt. |
| 6 */ |
| 7 #ifndef VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_ |
| 8 #define VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_ |
| 9 |
| 10 #include <getopt.h> |
| 11 #include <stdint.h> |
| 12 #include "cgptlib.h" |
| 13 |
| 14 enum { |
| 15 CGPT_OK = 0, |
| 16 CGPT_FAILED, /* generic error */ |
| 17 }; |
| 18 |
| 19 #define NOT_INITED (-1) /* to indicated a signed integer is not initialed. */ |
| 20 |
| 21 #define ARRAY_COUNT(array) (sizeof(array)/sizeof((array)[0])) |
| 22 |
| 23 /* 'struct option' of getopt_long() is not enough for our usage. |
| 24 * Therefore, we define the extra information to make option parsing |
| 25 * more organizable. |
| 26 * Note that please make sure every entry in struct option is mapped into an |
| 27 * individual entry in this struct. */ |
| 28 struct option_details { |
| 29 char *comment; |
| 30 |
| 31 /* If has_arg is 'required_argument', 'validator' is called to check whether |
| 32 * the 'argument' is valid or not. Once the argument is valid, the value is |
| 33 * stored in 'parsed'. |
| 34 * |
| 35 * If has_arg is 'no_argument', 'validator' is called to load 'valid_range' |
| 36 * into 'parsed' ('argument' is 0 in this case). Since getopt_long() only |
| 37 * supports integer type for 'flag' and 'val', this can support for any type. |
| 38 * |
| 39 * If has_arg is 'optional_argument', like 'required_argument', 'validator' is |
| 40 * called to check if 'argument' is valid or not. 'argument' indicates if |
| 41 * argument is present or not. |
| 42 * |
| 43 * 'validator' returns CGPT_OK if argument is valid; otherwise CGPT_FAILED |
| 44 * if invalid. */ |
| 45 int (*validator)(const char *argument, void *valid_range, void *parsed); |
| 46 void *valid_range; /* The structure passed to validator. */ |
| 47 void *parsed; /* The structure passed to validator. */ |
| 48 }; |
| 49 |
| 50 /* This is a special 'validator'. It assists those options without an argument, |
| 51 * i.e. help, to indicate the option is present. */ |
| 52 int AssignTrue(const char *argument, void *pointer, void *integer); |
| 53 |
| 54 struct number_range { |
| 55 int max; |
| 56 int min; |
| 57 }; |
| 58 |
| 59 /* Validator function. Returns 1 if 'argument' is between 'max' and 'min' |
| 60 * in 'valid_range'. */ |
| 61 int InNumberRange(const char *argument, void *valid_range, void *parsed); |
| 62 |
| 63 void ShowOptions(const struct option *opts, |
| 64 const struct option_details *details, |
| 65 const int num); |
| 66 |
| 67 /* Handles all options from given argc and argv. This function supports both |
| 68 * short and long options. |
| 69 * |
| 70 * Assumptions: |
| 71 * 1. every short option has a corresponding long option and the short option |
| 72 * is equal to 'val' of that long option. |
| 73 * 2. every entry in 'options' has a corresponding entry in 'details'. |
| 74 * One by one and in order. |
| 75 * |
| 76 * Returns CGPT_OK if given options in argv are good, otherwise CGPT_FAILED. |
| 77 * Note that the global variable 'optind' points to next non-option after |
| 78 * this function returns. |
| 79 */ |
| 80 int HandleOptions(const int argc, |
| 81 char *const *argv, |
| 82 const char *short_option, |
| 83 const int option_count, |
| 84 const struct option *options, |
| 85 const struct option_details *details); |
| 86 |
| 87 struct drive; |
| 88 int OpenDriveInLastArgument(const int argc, |
| 89 char *const *argv, |
| 90 struct drive *drive); |
| 91 |
| 92 /* Describes the drive storing the GPT. */ |
| 93 struct drive { |
| 94 int inited; /* indicated if this structure is valid */ |
| 95 int fd; /* file descriptor */ |
| 96 uint64_t size; /* total size (in bytes) */ |
| 97 GptData gpt; |
| 98 }; |
| 99 |
| 100 extern const char* progname; |
| 101 |
| 102 /* Given a hard drive path, this function loads GPT sectors from that drive, |
| 103 * and fills 'drive' structure. All memory allocated in drive_open() will be |
| 104 * freed at drive_close(). |
| 105 * |
| 106 * If 'drive_path' starts with '/', it is treated as absolute path. |
| 107 * If 'drive_path' starts with '.', it is treated as relative path. |
| 108 * Otherwise, it will be prepended with '/dev/' to comply with gpt. |
| 109 * |
| 110 * Returns CGPT_FAILED if any error happens. |
| 111 * Returns CGPT_OK if success and information are stored in 'drive'. |
| 112 */ |
| 113 int DriveOpen(const char *drive_path, struct drive *drive); |
| 114 int DriveClose(struct drive *drive); |
| 115 |
| 116 /* Function declarations for commands. |
| 117 * The return value of these functions is passed to main()'s exit value. */ |
| 118 int CgptAttribute(int argc, char *argv[]); |
| 119 int CgptShow(int argc, char *argv[]); |
| 120 |
| 121 #endif /* VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_ */ |
OLD | NEW |