summaryrefslogtreecommitdiff
path: root/utility/cgpt/cgpt.h
blob: 1b837efeedb10c040544a4102bdd8ef6e28eeef5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Header file for cgpt.
 */
#ifndef VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_
#define VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_

#include <getopt.h>
#include <stdint.h>
#include "cgptlib.h"

enum {
  CGPT_OK = 0,
  CGPT_FAILED,  /* generic error */
};

#define NOT_INITED (-1)  /* to indicated a signed integer is not initialed. */

#define ARRAY_COUNT(array) (sizeof(array)/sizeof((array)[0]))

/* 'struct option' of getopt_long() is not enough for our usage.
 * Therefore, we define the extra information to make option parsing
 * more organizable.
 * Note that please make sure every entry in struct option is mapped into an
 * individual entry in this struct. */
struct option_details {
  char *comment;

  /* If has_arg is 'required_argument', 'validator' is called to check whether
   * the 'argument' is valid or not. Once the argument is valid, the value is
   * stored in 'parsed'.
   *
   * If has_arg is 'no_argument', 'validator' is called to load 'valid_range'
   * into 'parsed' ('argument' is 0 in this case). Since getopt_long() only
   * supports integer type for 'flag' and 'val', this can support for any type.
   *
   * If has_arg is 'optional_argument', like 'required_argument', 'validator' is
   * called to check if 'argument' is valid or not. 'argument' indicates if
   * argument is present or not.
   *
   * 'validator' returns CGPT_OK if argument is valid; otherwise CGPT_FAILED
   * if invalid. */
  int (*validator)(const char *argument, void *valid_range, void *parsed);
  void *valid_range;  /* The structure passed to validator. */
  void *parsed;  /* The structure passed to validator. */
};

/* This is a special 'validator'. It assists those options without an argument,
 * i.e. help, to indicate the option is present. */
int AssignTrue(const char *argument, void *pointer, void *integer);

struct number_range {
  int max;
  int min;
};

/* Validator function. Returns 1 if 'argument' is between 'max' and 'min'
 * in 'valid_range'. */
int InNumberRange(const char *argument, void *valid_range, void *parsed);

void ShowOptions(const struct option *opts,
                 const struct option_details *details,
                 const int num);

/* Handles all options from given argc and argv. This function supports both
 * short and long options.
 *
 * Assumptions:
 *   1. every short option has a corresponding long option and the short option
 *      is equal to 'val' of that long option.
 *   2. every entry in 'options' has a corresponding entry in 'details'.
 *      One by one and in order.
 *
 * Returns CGPT_OK if given options in argv are good, otherwise CGPT_FAILED.
 * Note that the global variable 'optind' points to next non-option after
 * this function returns.
 */
int HandleOptions(const int argc,
                  char *const *argv,
                  const char *short_option,
                  const int option_count,
                  const struct option *options,
                  const struct option_details *details);

struct drive;
int OpenDriveInLastArgument(const int argc,
                            char *const *argv,
                            struct drive *drive);

/* Describes the drive storing the GPT. */
struct drive {
  int inited;       /* indicated if this structure is valid */
  int fd;           /* file descriptor */
  uint64_t size;    /* total size (in bytes) */
  GptData gpt;
};

extern const char* progname;

/* Given a hard drive path, this function loads GPT sectors from that drive,
 * and fills 'drive' structure. All memory allocated in drive_open() will be
 * freed at drive_close().
 *
 * If 'drive_path' starts with '/', it is treated as absolute path.
 * If 'drive_path' starts with '.', it is treated as relative path.
 * Otherwise, it will be prepended with '/dev/' to comply with gpt.
 *
 * Returns CGPT_FAILED if any error happens.
 * Returns CGPT_OK if success and information are stored in 'drive'.
 */
int DriveOpen(const char *drive_path, struct drive *drive);
int DriveClose(struct drive *drive);

/* Function declarations for commands.
 * The return value of these functions is passed to main()'s exit value. */
int CgptAttribute(int argc, char *argv[]);
int CgptRepair(int argc, char *argv[]);
int CgptShow(int argc, char *argv[]);

#endif  /* VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_ */