[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>
|
---|
| 48 |
|
---|
[271e24a] | 49 | #include "hdisk.h"
|
---|
[ec50ac4a] | 50 | #include "func_mbr.h"
|
---|
| 51 | #include "func_gpt.h"
|
---|
| 52 |
|
---|
[271e24a] | 53 | int interact(service_id_t dev_handle);
|
---|
[ec50ac4a] | 54 | void print_help(void);
|
---|
[271e24a] | 55 | void fill_table_funcs(void);
|
---|
| 56 | void free_table(void);
|
---|
[ec50ac4a] | 57 |
|
---|
[271e24a] | 58 | static table_t table;
|
---|
[ec50ac4a] | 59 |
|
---|
| 60 | int main(int argc, char ** argv)
|
---|
| 61 | {
|
---|
| 62 | if (argc == 1) {
|
---|
| 63 | printf("I'd like to have an argument, please.\n");
|
---|
| 64 | return 1;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | int rc;
|
---|
| 68 | service_id_t dev_handle;
|
---|
| 69 |
|
---|
| 70 | rc = loc_service_get_id(argv[1], &dev_handle, IPC_FLAG_BLOCKING);
|
---|
| 71 | if (rc != EOK) {
|
---|
| 72 | printf("Unknown device. Exiting.\n");
|
---|
| 73 | return -1;
|
---|
| 74 | }
|
---|
[271e24a] | 75 |
|
---|
| 76 | init_table();
|
---|
| 77 |
|
---|
[ec50ac4a] | 78 | mbr_t * mbr = mbr_read_mbr(dev_handle);
|
---|
| 79 | if(mbr == NULL) {
|
---|
| 80 | printf("Failed to read the Master Boot Record.\n" \
|
---|
[271e24a] | 81 | "Either memory allocation or disk access failed. Exiting.\n");
|
---|
[ec50ac4a] | 82 | return -1;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | if(mbr_is_mbr(mbr)) {
|
---|
[271e24a] | 86 | table.layout = LYT_MBR;
|
---|
| 87 | set_table_mbr(mbr);
|
---|
| 88 | mbr_partitions_t * parts = mbr_read_partitions(mbr);
|
---|
[ec50ac4a] | 89 | if(parts == NULL) {
|
---|
| 90 | printf("Failed to read and parse partitions.\n" \
|
---|
[271e24a] | 91 | "Creating new partition table.");
|
---|
| 92 | parts = mbr_alloc_partitions();
|
---|
[ec50ac4a] | 93 | }
|
---|
[271e24a] | 94 | set_table_mbr_parts(parts);
|
---|
| 95 | fill_table_funcs();
|
---|
[ec50ac4a] | 96 | } else {
|
---|
[271e24a] | 97 | table.layout = LYT_GPT;
|
---|
| 98 | mbr_free_mbr(mbr);
|
---|
| 99 | gpt_t * gpt = gpt_read_gpt_header(dev_handle);
|
---|
| 100 | if(gpt == NULL) {
|
---|
| 101 | printf("Failed to read and parse GPT header. Exiting.\n");
|
---|
| 102 | return -1;
|
---|
| 103 | }
|
---|
| 104 | set_table_gpt(gpt);
|
---|
| 105 | gpt_partitions_t * parts = gpt_read_partitions(gpt);
|
---|
| 106 | if(parts == NULL) {
|
---|
| 107 | printf("Failed to read and parse partitions.\n" \
|
---|
| 108 | "Creating new partition table.");
|
---|
| 109 | //parts = gpt_alloc_partitions();
|
---|
| 110 | }
|
---|
| 111 | set_table_gpt_parts(parts);
|
---|
| 112 | fill_table_funcs();
|
---|
[ec50ac4a] | 113 | }
|
---|
| 114 |
|
---|
[271e24a] | 115 | rc = interact(dev_handle);
|
---|
[ec50ac4a] | 116 |
|
---|
[271e24a] | 117 | free_table();
|
---|
[ec50ac4a] | 118 |
|
---|
| 119 | return rc;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[271e24a] | 122 | /*
|
---|
[ec50ac4a] | 123 | int get_input(tinput_t * in, char ** str)
|
---|
| 124 | {
|
---|
| 125 | int c;
|
---|
| 126 | size_tat
|
---|
| 127 |
|
---|
| 128 | pos = 0;
|
---|
| 129 | size_t size = 256;
|
---|
| 130 |
|
---|
| 131 | *str = malloc(size * sizeof(char));
|
---|
| 132 | if (*str == NULL)
|
---|
| 133 | return ENOMEM;
|
---|
| 134 |
|
---|
| 135 | while ((c = getchar()) != '\n') {
|
---|
| 136 | if (c >= 32 && c <= 126) { //a printable character
|
---|
| 137 |
|
---|
| 138 | (*str)[pos] = c;
|
---|
| 139 | ++pos;
|
---|
| 140 | putchar(c);
|
---|
| 141 |
|
---|
| 142 | if (pos == size) {
|
---|
| 143 | char * temp = malloc(2 * size * sizeof(char));
|
---|
| 144 | memcpy(temp, *str, size);
|
---|
| 145 | free(*str);
|
---|
| 146 | *str = temp;
|
---|
| 147 | size *= 2;
|
---|
| 148 | }
|
---|
| 149 | } else if (c == 8) { //backspace
|
---|
| 150 | (*str)[pos] = 0;
|
---|
| 151 | --pos;
|
---|
| 152 | putchar(c);
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | putchar('\n');
|
---|
| 157 |
|
---|
| 158 | (*str)[pos] = 0;
|
---|
| 159 |
|
---|
| 160 | return EOK;
|
---|
| 161 | }
|
---|
[271e24a] | 162 | */
|
---|
[ec50ac4a] | 163 |
|
---|
| 164 |
|
---|
| 165 | /** Interact with user */
|
---|
[271e24a] | 166 | int interact(service_id_t dev_handle)
|
---|
[ec50ac4a] | 167 | {
|
---|
| 168 | //int rc;
|
---|
[271e24a] | 169 | int input;
|
---|
[ec50ac4a] | 170 | tinput_t * in;
|
---|
| 171 |
|
---|
| 172 | in = tinput_new();
|
---|
| 173 | if (in == NULL) {
|
---|
| 174 | printf("Failed initing input. Free some memory.\n");
|
---|
[271e24a] | 175 | return ENOMEM;
|
---|
[ec50ac4a] | 176 | }
|
---|
[30440ed] | 177 | tinput_set_prompt(in, "");
|
---|
[ec50ac4a] | 178 |
|
---|
| 179 | printf("Welcome to hdisk.\nType 'h' for help.\n");
|
---|
| 180 |
|
---|
| 181 | //printf("# ");
|
---|
[271e24a] | 182 | //input = getchar();
|
---|
[ec50ac4a] | 183 | //printf("%c\n", input);
|
---|
| 184 |
|
---|
| 185 | while (1) {
|
---|
| 186 |
|
---|
[30440ed] | 187 | printf("# ");
|
---|
[271e24a] | 188 | input = getchar();
|
---|
[30440ed] | 189 | printf("%c\n", input);
|
---|
[271e24a] | 190 |
|
---|
| 191 |
|
---|
| 192 | //rc = tinput_read(in, &str);
|
---|
| 193 | //if (rc == ENOENT) {
|
---|
| 194 | //// User requested exit
|
---|
| 195 | //putchar('\n');
|
---|
| 196 | //return rc;
|
---|
| 197 | //}
|
---|
| 198 | //if (rc != EOK) {
|
---|
| 199 | //printf("Failed reading input. Exiting...\n");
|
---|
| 200 | //return rc;
|
---|
| 201 | //}
|
---|
| 202 | //// Check for empty input.
|
---|
| 203 | //if (str_cmp(str, "") == 0)
|
---|
| 204 | //continue;
|
---|
| 205 |
|
---|
| 206 | switch(input) {
|
---|
[ec50ac4a] | 207 | case 'a':
|
---|
[271e24a] | 208 | table.add_part(in, &table.data);
|
---|
[ec50ac4a] | 209 | break;
|
---|
| 210 | case 'd':
|
---|
[271e24a] | 211 | table.delete_part(in, &table.data);
|
---|
[ec50ac4a] | 212 | break;
|
---|
[30440ed] | 213 | case 'e':
|
---|
| 214 | table.extra_funcs(in, dev_handle, &table.data);
|
---|
| 215 | break;
|
---|
[ec50ac4a] | 216 | case 'h':
|
---|
| 217 | print_help();
|
---|
| 218 | break;
|
---|
| 219 | case 'p':
|
---|
[271e24a] | 220 | table.print_parts(&table.data);
|
---|
[ec50ac4a] | 221 | break;
|
---|
| 222 | case 'q':
|
---|
| 223 | putchar('\n');
|
---|
[271e24a] | 224 | goto end;
|
---|
[ec50ac4a] | 225 | case 'w':
|
---|
[271e24a] | 226 | table.write_parts(dev_handle, &table.data);
|
---|
[ec50ac4a] | 227 | break;
|
---|
| 228 | default:
|
---|
| 229 | printf("Unknown command. Try 'h' for help.\n");
|
---|
| 230 | break;
|
---|
| 231 | }
|
---|
| 232 | //printf("# ");
|
---|
| 233 | //input = getchar();
|
---|
| 234 | //printf("%c\n", input);
|
---|
| 235 |
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[271e24a] | 238 | end:
|
---|
[ec50ac4a] | 239 | tinput_destroy(in);
|
---|
| 240 |
|
---|
[271e24a] | 241 | return EOK;
|
---|
[ec50ac4a] | 242 | }
|
---|
| 243 |
|
---|
| 244 | void print_help(void)
|
---|
| 245 | {
|
---|
| 246 | printf(
|
---|
| 247 | "\t 'a' \t\t Add partition.\n"
|
---|
| 248 | "\t 'd' \t\t Delete partition.\n"
|
---|
| 249 | "\t 'h' \t\t Prints help. See help for more.\n" \
|
---|
[271e24a] | 250 | "\t 'p' \t\t Prints the table contents.\n" \
|
---|
| 251 | "\t 'w' \t\t Write table to disk.\n" \
|
---|
[ec50ac4a] | 252 | "\t 'q' \t\t Quit.\n" \
|
---|
| 253 | );
|
---|
| 254 |
|
---|
| 255 | }
|
---|
| 256 |
|
---|
[271e24a] | 257 | void fill_table_funcs(void)
|
---|
[ec50ac4a] | 258 | {
|
---|
[271e24a] | 259 | switch(table.layout) {
|
---|
| 260 | case LYT_MBR:
|
---|
| 261 | table.add_part = add_mbr_part;
|
---|
| 262 | table.delete_part = delete_mbr_part;
|
---|
| 263 | table.print_parts = print_mbr_parts;
|
---|
| 264 | table.write_parts = write_mbr_parts;
|
---|
[30440ed] | 265 | table.extra_funcs = extra_mbr_funcs;
|
---|
[271e24a] | 266 | break;
|
---|
| 267 | case LYT_GPT:
|
---|
| 268 | table.add_part = add_gpt_part;
|
---|
| 269 | table.delete_part = delete_gpt_part;
|
---|
| 270 | table.print_parts = print_gpt_parts;
|
---|
| 271 | table.write_parts = write_gpt_parts;
|
---|
[30440ed] | 272 | table.extra_funcs = extra_gpt_funcs;
|
---|
[271e24a] | 273 | break;
|
---|
| 274 | default:
|
---|
[ec50ac4a] | 275 | break;
|
---|
| 276 | }
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[271e24a] | 279 | void free_table(void)
|
---|
[ec50ac4a] | 280 | {
|
---|
[271e24a] | 281 | switch(table.layout) {
|
---|
| 282 | case LYT_MBR:
|
---|
| 283 | mbr_free_partitions(table.data.mbr.parts);
|
---|
| 284 | mbr_free_mbr(table.data.mbr.mbr);
|
---|
| 285 | break;
|
---|
| 286 | case LYT_GPT:
|
---|
| 287 | gpt_free_partitions(table.data.gpt.parts);
|
---|
| 288 | gpt_free_gpt(table.data.gpt.gpt);
|
---|
| 289 | break;
|
---|
| 290 | default:
|
---|
| 291 | break;
|
---|
[ec50ac4a] | 292 | }
|
---|
| 293 | }
|
---|
| 294 |
|
---|