[ec50ac4a] | 1 | /*
|
---|
[6453e306] | 2 | * Copyright (c) 2012-2013 Dominik Taborsky
|
---|
[ec50ac4a] | 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 |
|
---|
[6453e306] | 29 | /** @addtogroup hdisk
|
---|
[ec50ac4a] | 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>
|
---|
[271e24a] | 49 | #include "hdisk.h"
|
---|
[9bda5d90] | 50 | #include "input.h"
|
---|
[ec50ac4a] | 51 | #include "func_gpt.h"
|
---|
[9bda5d90] | 52 | #include "func_mbr.h"
|
---|
| 53 | #include "func_none.h"
|
---|
[ec50ac4a] | 54 |
|
---|
[6453e306] | 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 *);
|
---|
[a2aa81cb] | 64 |
|
---|
| 65 | static label_t label;
|
---|
[ec50ac4a] | 66 |
|
---|
[6453e306] | 67 | int main(int argc, char *argv[])
|
---|
[ec50ac4a] | 68 | {
|
---|
| 69 | if (argc == 1) {
|
---|
[a2aa81cb] | 70 | printf("Missing argument. Please specify a device to operate on.\n");
|
---|
[6453e306] | 71 | return 1;
|
---|
[ec50ac4a] | 72 | }
|
---|
[a2aa81cb] | 73 |
|
---|
[ec50ac4a] | 74 | service_id_t dev_handle;
|
---|
[6453e306] | 75 | int rc = loc_service_get_id(argv[1], &dev_handle, IPC_FLAG_BLOCKING);
|
---|
[ec50ac4a] | 76 | if (rc != EOK) {
|
---|
| 77 | printf("Unknown device. Exiting.\n");
|
---|
[6453e306] | 78 | return 2;
|
---|
[ec50ac4a] | 79 | }
|
---|
[a2aa81cb] | 80 |
|
---|
| 81 | init_label();
|
---|
[8c95dff] | 82 | label.device = dev_handle;
|
---|
[a2aa81cb] | 83 |
|
---|
[8c95dff] | 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));
|
---|
[6453e306] | 87 | return 3;
|
---|
[8c95dff] | 88 | }
|
---|
| 89 |
|
---|
[6453e306] | 90 | aoff64_t blocks;
|
---|
| 91 | rc = block_get_nblocks(dev_handle, &blocks);
|
---|
[8c95dff] | 92 | block_fini(dev_handle);
|
---|
| 93 | if (rc != EOK) {
|
---|
[6453e306] | 94 | printf("Error while getting number of blocks: %d - %s.\n",
|
---|
| 95 | rc, str_error(rc));
|
---|
| 96 | return 4;
|
---|
[8c95dff] | 97 | }
|
---|
| 98 |
|
---|
[6453e306] | 99 | label.blocks = blocks;
|
---|
[8c95dff] | 100 |
|
---|
| 101 | rc = try_read_mbr();
|
---|
[a2aa81cb] | 102 | if (rc == EOK)
|
---|
| 103 | goto interact;
|
---|
| 104 |
|
---|
[8c95dff] | 105 | free_label();
|
---|
| 106 |
|
---|
| 107 | rc = try_read_gpt();
|
---|
[a2aa81cb] | 108 | if (rc == EOK)
|
---|
| 109 | goto interact;
|
---|
| 110 |
|
---|
| 111 | printf("No label recognized. Create a new one.\n");
|
---|
[8c95dff] | 112 | construct_label(LYT_NONE);
|
---|
[700f89e] | 113 |
|
---|
| 114 | interact:
|
---|
[6453e306] | 115 | return interact();
|
---|
[ec50ac4a] | 116 | }
|
---|
| 117 |
|
---|
| 118 | /** Interact with user */
|
---|
[6453e306] | 119 | int interact(void)
|
---|
[ec50ac4a] | 120 | {
|
---|
[6453e306] | 121 | tinput_t *in = tinput_new();
|
---|
[ec50ac4a] | 122 | if (in == NULL) {
|
---|
| 123 | printf("Failed initing input. Free some memory.\n");
|
---|
[271e24a] | 124 | return ENOMEM;
|
---|
[ec50ac4a] | 125 | }
|
---|
[30440ed] | 126 | tinput_set_prompt(in, "");
|
---|
[700f89e] | 127 |
|
---|
[ec50ac4a] | 128 | printf("Welcome to hdisk.\nType 'h' for help.\n");
|
---|
[700f89e] | 129 |
|
---|
[6453e306] | 130 | while (true) {
|
---|
[30440ed] | 131 | printf("# ");
|
---|
[6453e306] | 132 | int input = getchar();
|
---|
[30440ed] | 133 | printf("%c\n", input);
|
---|
[700f89e] | 134 |
|
---|
[0435fe41] | 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':
|
---|
[8c95dff] | 143 | label.extra_funcs(&label, in);
|
---|
[0435fe41] | 144 | break;
|
---|
| 145 | case 'f':
|
---|
[8c95dff] | 146 | free_label();
|
---|
[0435fe41] | 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':
|
---|
[8c95dff] | 168 | label.read_parts(&label);
|
---|
[0435fe41] | 169 | case 'w':
|
---|
[8c95dff] | 170 | label.write_parts(&label);
|
---|
[0435fe41] | 171 | break;
|
---|
| 172 | default:
|
---|
| 173 | printf("Unknown command. Try 'h' for help.\n");
|
---|
| 174 | break;
|
---|
[ec50ac4a] | 175 | }
|
---|
| 176 | }
|
---|
[700f89e] | 177 |
|
---|
[271e24a] | 178 | end:
|
---|
[ec50ac4a] | 179 | tinput_destroy(in);
|
---|
[271e24a] | 180 | return EOK;
|
---|
[ec50ac4a] | 181 | }
|
---|
| 182 |
|
---|
| 183 | void print_help(void)
|
---|
| 184 | {
|
---|
| 185 | printf(
|
---|
[6453e306] | 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");
|
---|
[ec50ac4a] | 197 | }
|
---|
| 198 |
|
---|
[6453e306] | 199 | void select_label_format(tinput_t *in)
|
---|
[700f89e] | 200 | {
|
---|
| 201 | printf("Available formats are: \n"
|
---|
[6453e306] | 202 | "1) MBR\n"
|
---|
| 203 | "2) GPT\n");
|
---|
[700f89e] | 204 |
|
---|
| 205 | uint8_t val = get_input_uint8(in);
|
---|
[0435fe41] | 206 | switch (val) {
|
---|
| 207 | case 1:
|
---|
| 208 | construct_label(LYT_MBR);
|
---|
| 209 | break;
|
---|
| 210 | case 2:
|
---|
| 211 | construct_label(LYT_GPT);
|
---|
| 212 | break;
|
---|
[8c95dff] | 213 | default:
|
---|
| 214 | construct_label(LYT_NONE);
|
---|
| 215 | break;
|
---|
[700f89e] | 216 | }
|
---|
| 217 | }
|
---|
| 218 |
|
---|
[6e8e4e19] | 219 | void construct_label(layouts_t layout)
|
---|
[ec50ac4a] | 220 | {
|
---|
[0435fe41] | 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;
|
---|
[ec50ac4a] | 234 | }
|
---|
| 235 | }
|
---|
| 236 |
|
---|
[a2aa81cb] | 237 | void free_label(void)
|
---|
[ec50ac4a] | 238 | {
|
---|
[6e8e4e19] | 239 | label.destroy_label(&label);
|
---|
[a2aa81cb] | 240 | }
|
---|
| 241 |
|
---|
[6453e306] | 242 | int try_read(void)
|
---|
[a2aa81cb] | 243 | {
|
---|
[8c95dff] | 244 | return label.read_parts(&label);
|
---|
[ec50ac4a] | 245 | }
|
---|
[a2aa81cb] | 246 |
|
---|
[6453e306] | 247 | int try_read_mbr(void)
|
---|
[a2aa81cb] | 248 | {
|
---|
[6e8e4e19] | 249 | construct_label(LYT_MBR);
|
---|
[8c95dff] | 250 | return try_read();
|
---|
[a2aa81cb] | 251 | }
|
---|
| 252 |
|
---|
[6453e306] | 253 | int try_read_gpt(void)
|
---|
[a2aa81cb] | 254 | {
|
---|
[6e8e4e19] | 255 | construct_label(LYT_GPT);
|
---|
[8c95dff] | 256 | return try_read();
|
---|
[a2aa81cb] | 257 | }
|
---|
| 258 |
|
---|
[6e8e4e19] | 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 | }
|
---|