| 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>
|
|---|
| 40 | #include <block.h>
|
|---|
| 41 | #include <errno.h>
|
|---|
| 42 | #include <stdlib.h>
|
|---|
| 43 | #include <assert.h>
|
|---|
| 44 | #include <str.h>
|
|---|
| 45 | #include <libmbr.h>
|
|---|
| 46 | #include <libgpt.h>
|
|---|
| 47 | #include <tinput.h>
|
|---|
| 48 | #include <str_error.h>
|
|---|
| 49 | #include "hdisk.h"
|
|---|
| 50 | #include "input.h"
|
|---|
| 51 | #include "func_gpt.h"
|
|---|
| 52 | #include "func_mbr.h"
|
|---|
| 53 | #include "func_none.h"
|
|---|
| 54 |
|
|---|
| 55 | static int interact(void);
|
|---|
| 56 | static void print_help(void);
|
|---|
| 57 | static void select_label_format(tinput_t *);
|
|---|
| 58 | static void construct_label(layouts_t);
|
|---|
| 59 | static void free_label(void);
|
|---|
| 60 | static int try_read(void);
|
|---|
| 61 | static int try_read_mbr(void);
|
|---|
| 62 | static int try_read_gpt(void);
|
|---|
| 63 | static void set_alignment(tinput_t *);
|
|---|
| 64 |
|
|---|
| 65 | static label_t label;
|
|---|
| 66 |
|
|---|
| 67 | int main(int argc, char *argv[])
|
|---|
| 68 | {
|
|---|
| 69 | if (argc == 1) {
|
|---|
| 70 | printf("Missing argument. Please specify a device to operate on.\n");
|
|---|
| 71 | return 1;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | service_id_t dev_handle;
|
|---|
| 75 | int rc = loc_service_get_id(argv[1], &dev_handle, IPC_FLAG_BLOCKING);
|
|---|
| 76 | if (rc != EOK) {
|
|---|
| 77 | printf("Unknown device. Exiting.\n");
|
|---|
| 78 | return 2;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | init_label();
|
|---|
| 82 | label.device = dev_handle;
|
|---|
| 83 |
|
|---|
| 84 | rc = block_init(EXCHANGE_ATOMIC, dev_handle, 512);
|
|---|
| 85 | if (rc != EOK) {
|
|---|
| 86 | printf("Error during libblock init: %d - %s.\n", rc, str_error(rc));
|
|---|
| 87 | return 3;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | aoff64_t blocks;
|
|---|
| 91 | rc = block_get_nblocks(dev_handle, &blocks);
|
|---|
| 92 | block_fini(dev_handle);
|
|---|
| 93 | if (rc != EOK) {
|
|---|
| 94 | printf("Error while getting number of blocks: %d - %s.\n",
|
|---|
| 95 | rc, str_error(rc));
|
|---|
| 96 | return 4;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | label.blocks = blocks;
|
|---|
| 100 |
|
|---|
| 101 | rc = try_read_mbr();
|
|---|
| 102 | if (rc == EOK)
|
|---|
| 103 | goto interact;
|
|---|
| 104 |
|
|---|
| 105 | free_label();
|
|---|
| 106 |
|
|---|
| 107 | rc = try_read_gpt();
|
|---|
| 108 | if (rc == EOK)
|
|---|
| 109 | goto interact;
|
|---|
| 110 |
|
|---|
| 111 | printf("No label recognized. Create a new one.\n");
|
|---|
| 112 | construct_label(LYT_NONE);
|
|---|
| 113 |
|
|---|
| 114 | interact:
|
|---|
| 115 | return interact();
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | /** Interact with user */
|
|---|
| 119 | int interact(void)
|
|---|
| 120 | {
|
|---|
| 121 | tinput_t *in = tinput_new();
|
|---|
| 122 | if (in == NULL) {
|
|---|
| 123 | printf("Failed initing input. Free some memory.\n");
|
|---|
| 124 | return ENOMEM;
|
|---|
| 125 | }
|
|---|
| 126 | tinput_set_prompt(in, "");
|
|---|
| 127 |
|
|---|
| 128 | printf("Welcome to hdisk.\nType 'h' for help.\n");
|
|---|
| 129 |
|
|---|
| 130 | while (true) {
|
|---|
| 131 | printf("# ");
|
|---|
| 132 | int input = getchar();
|
|---|
| 133 | printf("%c\n", input);
|
|---|
| 134 |
|
|---|
| 135 | switch (input) {
|
|---|
| 136 | case 'a':
|
|---|
| 137 | label.add_part(&label, in);
|
|---|
| 138 | break;
|
|---|
| 139 | case 'd':
|
|---|
| 140 | label.delete_part(&label, in);
|
|---|
| 141 | break;
|
|---|
| 142 | case 'e':
|
|---|
| 143 | label.extra_funcs(&label, in);
|
|---|
| 144 | break;
|
|---|
| 145 | case 'f':
|
|---|
| 146 | free_label();
|
|---|
| 147 | select_label_format(in);
|
|---|
| 148 | break;
|
|---|
| 149 | case 'h':
|
|---|
| 150 | print_help();
|
|---|
| 151 | break;
|
|---|
| 152 | case 'l':
|
|---|
| 153 | set_alignment(in);
|
|---|
| 154 | break;
|
|---|
| 155 | case 'n':
|
|---|
| 156 | printf("Discarding label...\n");
|
|---|
| 157 | free_label();
|
|---|
| 158 | label.new_label(&label);
|
|---|
| 159 | break;
|
|---|
| 160 | case 'p':
|
|---|
| 161 | label.print_parts(&label);
|
|---|
| 162 | break;
|
|---|
| 163 | case 'q':
|
|---|
| 164 | putchar('\n');
|
|---|
| 165 | free_label();
|
|---|
| 166 | goto end;
|
|---|
| 167 | case 'r':
|
|---|
| 168 | label.read_parts(&label);
|
|---|
| 169 | case 'w':
|
|---|
| 170 | label.write_parts(&label);
|
|---|
| 171 | break;
|
|---|
| 172 | default:
|
|---|
| 173 | printf("Unknown command. Try 'h' for help.\n");
|
|---|
| 174 | break;
|
|---|
| 175 | }
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | end:
|
|---|
| 179 | tinput_destroy(in);
|
|---|
| 180 | return EOK;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | void print_help(void)
|
|---|
| 184 | {
|
|---|
| 185 | printf(
|
|---|
| 186 | "\t 'a' \t\t Add partition.\n"
|
|---|
| 187 | "\t 'd' \t\t Delete partition.\n"
|
|---|
| 188 | "\t 'e' \t\t Extra functions (per label format).\n"
|
|---|
| 189 | "\t 'f' \t\t Switch the format of the partition label.\n"
|
|---|
| 190 | "\t 'h' \t\t Prints help. See help for more.\n"
|
|---|
| 191 | "\t 'l' \t\t Set alignment.\n"
|
|---|
| 192 | "\t 'n' \t\t Create new label (discarding the old one).\n"
|
|---|
| 193 | "\t 'p' \t\t Prints label contents.\n"
|
|---|
| 194 | "\t 'q' \t\t Quit.\n"
|
|---|
| 195 | "\t 'r' \t\t Read label from disk.\n"
|
|---|
| 196 | "\t 'w' \t\t Write label to disk.\n");
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | void select_label_format(tinput_t *in)
|
|---|
| 200 | {
|
|---|
| 201 | printf("Available formats are: \n"
|
|---|
| 202 | "1) MBR\n"
|
|---|
| 203 | "2) GPT\n");
|
|---|
| 204 |
|
|---|
| 205 | uint8_t val = get_input_uint8(in);
|
|---|
| 206 | switch (val) {
|
|---|
| 207 | case 1:
|
|---|
| 208 | construct_label(LYT_MBR);
|
|---|
| 209 | break;
|
|---|
| 210 | case 2:
|
|---|
| 211 | construct_label(LYT_GPT);
|
|---|
| 212 | break;
|
|---|
| 213 | default:
|
|---|
| 214 | construct_label(LYT_NONE);
|
|---|
| 215 | break;
|
|---|
| 216 | }
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | void construct_label(layouts_t layout)
|
|---|
| 220 | {
|
|---|
| 221 | switch (layout) {
|
|---|
| 222 | case LYT_MBR:
|
|---|
| 223 | label.layout = LYT_MBR;
|
|---|
| 224 | construct_mbr_label(&label);
|
|---|
| 225 | break;
|
|---|
| 226 | case LYT_GPT:
|
|---|
| 227 | label.layout = LYT_GPT;
|
|---|
| 228 | construct_gpt_label(&label);
|
|---|
| 229 | break;
|
|---|
| 230 | default:
|
|---|
| 231 | label.layout = LYT_NONE;
|
|---|
| 232 | construct_none_label(&label);
|
|---|
| 233 | break;
|
|---|
| 234 | }
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | void free_label(void)
|
|---|
| 238 | {
|
|---|
| 239 | label.destroy_label(&label);
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | int try_read(void)
|
|---|
| 243 | {
|
|---|
| 244 | return label.read_parts(&label);
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | int try_read_mbr(void)
|
|---|
| 248 | {
|
|---|
| 249 | construct_label(LYT_MBR);
|
|---|
| 250 | return try_read();
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | int try_read_gpt(void)
|
|---|
| 254 | {
|
|---|
| 255 | construct_label(LYT_GPT);
|
|---|
| 256 | return try_read();
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | void set_alignment(tinput_t *in)
|
|---|
| 260 | {
|
|---|
| 261 | printf("Set alignment to sectors: ");
|
|---|
| 262 | label.alignment = get_input_uint32(in);
|
|---|
| 263 | printf("Alignment set to %u sectors.\n", label.alignment);
|
|---|
| 264 | }
|
|---|