[ec50ac4a] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012, 2013 Dominik Taborsky
|
---|
| 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 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup hdisk
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <ipc/bd.h>
|
---|
| 36 | #include <loc.h>
|
---|
| 37 | #include <async.h>
|
---|
| 38 | #include <stdio.h>
|
---|
| 39 | #include <ipc/services.h>
|
---|
[271e24a] | 40 | #include <block.h>
|
---|
[ec50ac4a] | 41 | #include <errno.h>
|
---|
| 42 | #include <stdlib.h>
|
---|
| 43 | #include <assert.h>
|
---|
| 44 | #include <str.h>
|
---|
| 45 | #include <libmbr.h>
|
---|
[271e24a] | 46 | #include <libgpt.h>
|
---|
[ec50ac4a] | 47 | #include <tinput.h>
|
---|
[8c95dff] | 48 | #include <str_error.h>
|
---|
[ec50ac4a] | 49 |
|
---|
[271e24a] | 50 | #include "hdisk.h"
|
---|
[9bda5d90] | 51 | #include "input.h"
|
---|
[ec50ac4a] | 52 | #include "func_gpt.h"
|
---|
[9bda5d90] | 53 | #include "func_mbr.h"
|
---|
| 54 | #include "func_none.h"
|
---|
[ec50ac4a] | 55 |
|
---|
[8c95dff] | 56 | int interact(void);
|
---|
[ec50ac4a] | 57 | void print_help(void);
|
---|
[a2aa81cb] | 58 | void select_label_format(tinput_t *);
|
---|
[6e8e4e19] | 59 | void construct_label(layouts_t);
|
---|
[a2aa81cb] | 60 | void free_label(void);
|
---|
[8c95dff] | 61 | int try_read(void);
|
---|
| 62 | int try_read_mbr(void);
|
---|
| 63 | int try_read_gpt(void);
|
---|
[6e8e4e19] | 64 | void set_alignment(tinput_t *);
|
---|
[a2aa81cb] | 65 |
|
---|
| 66 |
|
---|
| 67 | static label_t label;
|
---|
[ec50ac4a] | 68 |
|
---|
| 69 | int main(int argc, char ** argv)
|
---|
| 70 | {
|
---|
| 71 | if (argc == 1) {
|
---|
[a2aa81cb] | 72 | printf("Missing argument. Please specify a device to operate on.\n");
|
---|
| 73 | return -1;
|
---|
[ec50ac4a] | 74 | }
|
---|
[a2aa81cb] | 75 |
|
---|
[ec50ac4a] | 76 | int rc;
|
---|
| 77 | service_id_t dev_handle;
|
---|
[a2aa81cb] | 78 |
|
---|
[ec50ac4a] | 79 | rc = loc_service_get_id(argv[1], &dev_handle, IPC_FLAG_BLOCKING);
|
---|
| 80 | if (rc != EOK) {
|
---|
| 81 | printf("Unknown device. Exiting.\n");
|
---|
| 82 | return -1;
|
---|
| 83 | }
|
---|
[a2aa81cb] | 84 |
|
---|
| 85 | init_label();
|
---|
[8c95dff] | 86 | label.device = dev_handle;
|
---|
[a2aa81cb] | 87 |
|
---|
[8c95dff] | 88 | rc = block_init(EXCHANGE_ATOMIC, dev_handle, 512);
|
---|
| 89 | if (rc != EOK) {
|
---|
| 90 | printf("Error during libblock init: %d - %s.\n", rc, str_error(rc));
|
---|
| 91 | return -1;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | aoff64_t nblocks;
|
---|
| 95 | rc = block_get_nblocks(dev_handle, &nblocks);
|
---|
| 96 | block_fini(dev_handle);
|
---|
| 97 | if (rc != EOK) {
|
---|
| 98 | printf(LIBMBR_NAME ": Error while getting number of blocks: %d - %s.\n", rc, str_error(rc));
|
---|
| 99 | return -1;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | label.nblocks = nblocks;
|
---|
| 103 |
|
---|
| 104 | rc = try_read_mbr();
|
---|
[a2aa81cb] | 105 | if (rc == EOK)
|
---|
| 106 | goto interact;
|
---|
| 107 |
|
---|
[8c95dff] | 108 | free_label();
|
---|
| 109 |
|
---|
| 110 | rc = try_read_gpt();
|
---|
[a2aa81cb] | 111 | if (rc == EOK)
|
---|
| 112 | goto interact;
|
---|
| 113 |
|
---|
| 114 | printf("No label recognized. Create a new one.\n");
|
---|
[8c95dff] | 115 | construct_label(LYT_NONE);
|
---|
[700f89e] | 116 |
|
---|
| 117 | interact:
|
---|
[6e8e4e19] | 118 |
|
---|
[8c95dff] | 119 | rc = interact();
|
---|
[8f6c7785] | 120 |
|
---|
[ec50ac4a] | 121 | return rc;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | /** Interact with user */
|
---|
[8c95dff] | 125 | int interact()
|
---|
[ec50ac4a] | 126 | {
|
---|
[271e24a] | 127 | int input;
|
---|
[6e8e4e19] | 128 | tinput_t *in;
|
---|
[700f89e] | 129 |
|
---|
[ec50ac4a] | 130 | in = tinput_new();
|
---|
| 131 | if (in == NULL) {
|
---|
| 132 | printf("Failed initing input. Free some memory.\n");
|
---|
[271e24a] | 133 | return ENOMEM;
|
---|
[ec50ac4a] | 134 | }
|
---|
[30440ed] | 135 | tinput_set_prompt(in, "");
|
---|
[700f89e] | 136 |
|
---|
[ec50ac4a] | 137 | printf("Welcome to hdisk.\nType 'h' for help.\n");
|
---|
[700f89e] | 138 |
|
---|
[ec50ac4a] | 139 | while (1) {
|
---|
[30440ed] | 140 | printf("# ");
|
---|
[271e24a] | 141 | input = getchar();
|
---|
[30440ed] | 142 | printf("%c\n", input);
|
---|
[700f89e] | 143 |
|
---|
[0435fe41] | 144 | switch (input) {
|
---|
| 145 | case 'a':
|
---|
| 146 | label.add_part(&label, in);
|
---|
| 147 | break;
|
---|
| 148 | case 'd':
|
---|
| 149 | label.delete_part(&label, in);
|
---|
| 150 | break;
|
---|
| 151 | case 'e':
|
---|
[8c95dff] | 152 | label.extra_funcs(&label, in);
|
---|
[0435fe41] | 153 | break;
|
---|
| 154 | case 'f':
|
---|
[8c95dff] | 155 | free_label();
|
---|
[0435fe41] | 156 | select_label_format(in);
|
---|
| 157 | break;
|
---|
| 158 | case 'h':
|
---|
| 159 | print_help();
|
---|
| 160 | break;
|
---|
| 161 | case 'l':
|
---|
| 162 | set_alignment(in);
|
---|
| 163 | break;
|
---|
| 164 | case 'n':
|
---|
| 165 | printf("Discarding label...\n");
|
---|
| 166 | free_label();
|
---|
| 167 | label.new_label(&label);
|
---|
| 168 | break;
|
---|
| 169 | case 'p':
|
---|
| 170 | label.print_parts(&label);
|
---|
| 171 | break;
|
---|
| 172 | case 'q':
|
---|
| 173 | putchar('\n');
|
---|
| 174 | free_label();
|
---|
| 175 | goto end;
|
---|
| 176 | case 'r':
|
---|
[8c95dff] | 177 | label.read_parts(&label);
|
---|
[0435fe41] | 178 | case 'w':
|
---|
[8c95dff] | 179 | label.write_parts(&label);
|
---|
[0435fe41] | 180 | break;
|
---|
| 181 | default:
|
---|
| 182 | printf("Unknown command. Try 'h' for help.\n");
|
---|
| 183 | break;
|
---|
[ec50ac4a] | 184 | }
|
---|
| 185 | }
|
---|
[700f89e] | 186 |
|
---|
[271e24a] | 187 | end:
|
---|
[ec50ac4a] | 188 | tinput_destroy(in);
|
---|
[700f89e] | 189 |
|
---|
[271e24a] | 190 | return EOK;
|
---|
[ec50ac4a] | 191 | }
|
---|
| 192 |
|
---|
| 193 | void print_help(void)
|
---|
| 194 | {
|
---|
| 195 | printf(
|
---|
| 196 | "\t 'a' \t\t Add partition.\n"
|
---|
| 197 | "\t 'd' \t\t Delete partition.\n"
|
---|
[a2aa81cb] | 198 | "\t 'e' \t\t Extra functions (per label format).\n"
|
---|
[6e8e4e19] | 199 | "\t 'f' \t\t Switch the format of the partition label.\n"
|
---|
[700f89e] | 200 | "\t 'h' \t\t Prints help. See help for more.\n"
|
---|
[a2aa81cb] | 201 | "\t 'l' \t\t Set alignment.\n"
|
---|
| 202 | "\t 'n' \t\t Create new label (discarding the old one).\n"
|
---|
| 203 | "\t 'p' \t\t Prints label contents.\n"
|
---|
[700f89e] | 204 | "\t 'q' \t\t Quit.\n"
|
---|
[6e8e4e19] | 205 | "\t 'r' \t\t Read label from disk.\n"
|
---|
| 206 | "\t 'w' \t\t Write label to disk.\n"
|
---|
[ec50ac4a] | 207 | );
|
---|
| 208 |
|
---|
| 209 | }
|
---|
| 210 |
|
---|
[a2aa81cb] | 211 | void select_label_format(tinput_t * in)
|
---|
[700f89e] | 212 | {
|
---|
| 213 | printf("Available formats are: \n"
|
---|
| 214 | "1) MBR\n"
|
---|
| 215 | "2) GPT\n"
|
---|
[6e8e4e19] | 216 | );
|
---|
[700f89e] | 217 |
|
---|
| 218 | uint8_t val = get_input_uint8(in);
|
---|
[0435fe41] | 219 | switch (val) {
|
---|
| 220 | case 1:
|
---|
| 221 | construct_label(LYT_MBR);
|
---|
| 222 | break;
|
---|
| 223 | case 2:
|
---|
| 224 | construct_label(LYT_GPT);
|
---|
| 225 | break;
|
---|
[8c95dff] | 226 | default:
|
---|
| 227 | construct_label(LYT_NONE);
|
---|
| 228 | break;
|
---|
[700f89e] | 229 | }
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[6e8e4e19] | 232 | void construct_label(layouts_t layout)
|
---|
[ec50ac4a] | 233 | {
|
---|
[0435fe41] | 234 | switch (layout) {
|
---|
| 235 | case LYT_MBR:
|
---|
| 236 | label.layout = LYT_MBR;
|
---|
| 237 | construct_mbr_label(&label);
|
---|
| 238 | break;
|
---|
| 239 | case LYT_GPT:
|
---|
| 240 | label.layout = LYT_GPT;
|
---|
| 241 | construct_gpt_label(&label);
|
---|
| 242 | break;
|
---|
| 243 | default:
|
---|
| 244 | label.layout = LYT_NONE;
|
---|
| 245 | construct_none_label(&label);
|
---|
| 246 | break;
|
---|
[ec50ac4a] | 247 | }
|
---|
| 248 | }
|
---|
| 249 |
|
---|
[a2aa81cb] | 250 | void free_label(void)
|
---|
[ec50ac4a] | 251 | {
|
---|
[6e8e4e19] | 252 | label.destroy_label(&label);
|
---|
[a2aa81cb] | 253 | }
|
---|
| 254 |
|
---|
[8c95dff] | 255 | int try_read()
|
---|
[a2aa81cb] | 256 | {
|
---|
[8c95dff] | 257 |
|
---|
| 258 | return label.read_parts(&label);
|
---|
[ec50ac4a] | 259 | }
|
---|
[a2aa81cb] | 260 |
|
---|
[8c95dff] | 261 | int try_read_mbr()
|
---|
[a2aa81cb] | 262 | {
|
---|
[6e8e4e19] | 263 | construct_label(LYT_MBR);
|
---|
[8c95dff] | 264 | return try_read();
|
---|
[a2aa81cb] | 265 | }
|
---|
| 266 |
|
---|
[8c95dff] | 267 | int try_read_gpt()
|
---|
[a2aa81cb] | 268 | {
|
---|
[6e8e4e19] | 269 | construct_label(LYT_GPT);
|
---|
[8c95dff] | 270 | return try_read();
|
---|
[a2aa81cb] | 271 | }
|
---|
| 272 |
|
---|
[6e8e4e19] | 273 | void set_alignment(tinput_t *in)
|
---|
| 274 | {
|
---|
| 275 | printf("Set alignment to sectors: ");
|
---|
| 276 | label.alignment = get_input_uint32(in);
|
---|
| 277 | printf("Alignment set to %u sectors.\n", label.alignment);
|
---|
| 278 | }
|
---|
[a2aa81cb] | 279 |
|
---|
| 280 |
|
---|
| 281 |
|
---|