Changeset 271e24a in mainline for uspace/app/hdisk/hdisk.c
- Timestamp:
- 2013-03-24T00:12:25Z (11 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 30440ed
- Parents:
- ec50ac4a
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/hdisk/hdisk.c
rec50ac4a r271e24a 38 38 #include <stdio.h> 39 39 #include <ipc/services.h> 40 #include < libblock.h>40 #include <block.h> 41 41 #include <errno.h> 42 42 #include <stdlib.h> … … 44 44 #include <str.h> 45 45 #include <libmbr.h> 46 #include <libgpt.h> 46 47 #include <tinput.h> 47 48 49 #include "hdisk.h" 48 50 #include "func_mbr.h" 49 51 #include "func_gpt.h" 50 52 51 enum TABLES { 52 TBL_MBR, 53 TBL_GPT; 54 }; 55 56 static TABLES table; 57 58 int get_input(tinput_t * in, char ** str); 59 void interact(mbr_parts_t * partitions, mbr_t * mbr, service_id_t dev_handle); 53 int interact(service_id_t dev_handle); 60 54 void print_help(void); 61 62 63 int set_primary(tinput_t * in, mbr_parts_t * parts); 64 int add_logical(tinput_t * in, mbr_parts_t * parts); 65 int set_mbr_partition(tinput_t * in, mbr_parts_t * p); 55 void fill_table_funcs(void); 56 void free_table(void); 57 58 static table_t table; 66 59 67 60 int main(int argc, char ** argv) … … 80 73 return -1; 81 74 } 82 83 mbr_parts_t * parts = NULL; 84 gpt_header_t * g_hdr = NULL; 85 gpt_parts_t * g_parts = NULL; 86 75 76 init_table(); 77 87 78 mbr_t * mbr = mbr_read_mbr(dev_handle); 88 79 if(mbr == NULL) { 89 80 printf("Failed to read the Master Boot Record.\n" \ 90 "Either memory allocation or disk access failed. \n");81 "Either memory allocation or disk access failed. Exiting.\n"); 91 82 return -1; 92 83 } 93 84 94 85 if(mbr_is_mbr(mbr)) { 95 table = TBL_MBR; 96 parts = mbr_read_partitions(mbr); 86 table.layout = LYT_MBR; 87 set_table_mbr(mbr); 88 mbr_partitions_t * parts = mbr_read_partitions(mbr); 97 89 if(parts == NULL) { 98 90 printf("Failed to read and parse partitions.\n" \ 99 "Creating new partition table.") 100 } 91 "Creating new partition table."); 92 parts = mbr_alloc_partitions(); 93 } 94 set_table_mbr_parts(parts); 95 fill_table_funcs(); 101 96 } else { 102 table = TBL_GPT; 103 //g_hdr = gpt_read_header(); 104 //g_parts = gpt_read_partitions(); 105 } 106 107 108 rc = interact(partitions, mbr, dev_handle); 109 110 if(gpt) { 111 gpt_free_partitions(); 112 gpt_free_header(); 113 } else { 114 mbr_free_partitions(partitions); 97 table.layout = LYT_GPT; 115 98 mbr_free_mbr(mbr); 116 } 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(); 113 } 114 115 rc = interact(dev_handle); 116 117 free_table(); 117 118 118 119 return rc; 119 120 } 120 121 122 /* 121 123 int get_input(tinput_t * in, char ** str) 122 124 { … … 158 160 return EOK; 159 161 } 160 161 //int get_input(tinput_t * in, char ** str) 162 //{ 163 // int rc; 164 // printf("help1\n"); 165 // rc = tinput_read(in, str); 166 // if (rc == ENOENT) { 167 // /* User requested exit */ 168 // return EINTR; 169 // } 170 // printf("help2\n"); 171 // fflush(stdout); 172 // if (rc != EOK) { 173 // printf("Failed reading input. Cancelling...\n"); 174 // return rc; 175 // } 176 // printf("help3\n"); 177 // fflush(stdout); 178 // /* Check for empty input. */ 179 // if (str_cmp(*str, "") == 0) { 180 // printf("Canceled.\n"); 181 // return EINVAL; 182 // } 183 // printf("help4\n"); 184 // fflush(stdout); 185 // 186 // return EOK; 187 //} 162 */ 163 188 164 189 165 /** Interact with user */ 190 int interact_mbr(mbr_parts_t * partitions, mbr_t * mbr, service_id_t dev_handle) 191 { 192 char * str; 166 int interact(service_id_t dev_handle) 167 { 193 168 //int rc; 169 int input; 194 170 tinput_t * in; 195 171 … … 197 173 if (in == NULL) { 198 174 printf("Failed initing input. Free some memory.\n"); 199 return; 200 } 201 202 tinput_set_prompt(in, " "); 175 return ENOMEM; 176 } 203 177 204 178 printf("Welcome to hdisk.\nType 'h' for help.\n"); 205 179 206 180 //printf("# "); 207 //in t input = getchar();181 //input = getchar(); 208 182 //printf("%c\n", input); 209 183 210 184 while (1) { 211 185 212 / *printf("# ");213 in t input = getchar();214 printf("%c\n", input);215 */216 217 rc = tinput_read(in, &str);218 if (rc == ENOENT) {219 // User requested exit220 putchar('\n');221 return rc;222 }223 if (rc != EOK) {224 printf("Failed reading input. Exiting...\n");225 return rc;226 }227 // Check for empty input.228 if (str_cmp(str, "") == 0)229 continue;230 231 switch( *str) {186 //printf("# "); 187 input = getchar(); 188 //printf("%c\n", input); 189 190 191 //rc = tinput_read(in, &str); 192 //if (rc == ENOENT) { 193 //// User requested exit 194 //putchar('\n'); 195 //return rc; 196 //} 197 //if (rc != EOK) { 198 //printf("Failed reading input. Exiting...\n"); 199 //return rc; 200 //} 201 //// Check for empty input. 202 //if (str_cmp(str, "") == 0) 203 //continue; 204 205 switch(input) { 232 206 case 'a': 233 add_part(in, partitions);207 table.add_part(in, &table.data); 234 208 break; 235 209 case 'd': 236 delete_part(in, partitions);210 table.delete_part(in, &table.data); 237 211 break; 238 212 case 'h': … … 240 214 break; 241 215 case 'p': 242 print_MBR(partitions);216 table.print_parts(&table.data); 243 217 break; 244 218 case 'q': 245 219 putchar('\n'); 246 return;220 goto end; 247 221 case 'w': 248 write_parts(partitions, mbr, dev_handle);222 table.write_parts(dev_handle, &table.data); 249 223 break; 250 224 default: … … 258 232 } 259 233 234 end: 260 235 tinput_destroy(in); 261 236 262 return ;237 return EOK; 263 238 } 264 239 … … 269 244 "\t 'd' \t\t Delete partition.\n" 270 245 "\t 'h' \t\t Prints help. See help for more.\n" \ 271 "\t 'p' \t\t Prints the MBR contents.\n" \ 246 "\t 'p' \t\t Prints the table contents.\n" \ 247 "\t 'w' \t\t Write table to disk.\n" \ 272 248 "\t 'q' \t\t Quit.\n" \ 273 249 ); … … 275 251 } 276 252 277 //other functions 278 //FIXME: need error checking after input 279 int set_primary(tinput_t * in, part_t * parts) 280 { 281 char * str; 282 int rc; 283 uint32_t input = 5; 284 part_t * p = parts; 285 //char c_input[2]; 286 char * endptr; 287 288 // while (input > 3) { 289 // printf("Number of the primary partition to set (0-3): "); 290 // str_uint64(fgets(c_input, 1, stdin), &endptr, 10, true, &input); 291 // printf("%llu\n", input); 292 // } 293 294 if ((rc = get_input(in, &str)) != EOK) 295 return rc; 296 297 if (str_uint32_t(str, &endptr, 10, true, &input) != EOK || input > 3) { 298 printf("Invalid value. Canceled.\n"); 299 return EINVAL; 300 } 301 302 free(str); 303 304 for (; input > 0; --input) { 305 p = p->next; 306 } 307 308 set_partition(in, p); 309 310 return EOK; 311 } 312 313 int add_logical(tinput_t * in, part_t * parts) 314 { 315 int i; 316 part_t * p = parts; 317 part_t * ext = NULL; 318 319 //checking primary partitions for extended partition 320 for (i = 0; i < N_PRIMARY; ++i) { 321 if (p->type == PT_EXTENDED) { 322 ext = p; 323 break; 324 } 325 p = p->next; 326 } 327 328 if (ext == NULL) { 329 printf("Error: no extended partition.\n"); 330 return EINVAL; 331 } 332 333 while (p->next != NULL) { 334 p = p->next; 335 } 336 337 p->next = malloc(sizeof(part_t)); 338 if (p->next == NULL) { 339 printf("Error: not enough memory.\n"); 340 return ENOMEM; 341 } 342 343 p->next->ebr = malloc(sizeof(br_block_t)); 344 if (p->next->ebr == NULL) { 345 printf("Error: not enough memory.\n"); 346 return ENOMEM; 347 } 348 349 set_partition(in, p->next); 350 351 return EOK; 352 } 353 354 int set_mbr_partition(tinput_t * in, part_t * p) 355 { 356 char * str; 357 int rc; 358 uint8_t type; 359 char * endptr; 360 361 // while (type > 255) { 362 // printf("Set type (0-255): "); 363 // str_uint64(fgets(c_type, 3, stdin), &endptr, 10, true, &type); 364 // } 365 366 if ((rc = get_input(in, &str)) != EOK) 367 return rc; 368 369 if (str_uint8_t(str, &endptr, 10, true, &type) != EOK) { 370 printf("Invalid value. Canceled.\n"); 371 return EINVAL; 372 } 373 374 free(str); 375 376 ///TODO: there can only be one bootable partition; let's do it just like fdisk 377 printf("Bootable? (y/n): "); 378 int c = getchar(); 379 printf("%c\n", c); 380 if (c != 'y' && c != 'Y' && c != 'n' && c != 'N') { 381 printf("Invalid value. Cancelled."); 382 return EINVAL; 383 } 384 385 uint32_t sa, ea; 386 387 printf("Set starting address (number): "); 388 //str_uint64(fgets(c_sa, 21, stdin), &endptr, 10, true, &sa); 389 390 if ((rc = get_input(in, &str)) != EOK) 391 return rc; 392 393 if(str_uint32_t(str, &endptr, 10, true, &sa) != EOK) { 394 printf("Invalid value. Canceled.\n"); 395 return EINVAL; 396 } 397 398 free(str); 399 400 printf("Set end addres (number): "); 401 //str_uint64(fgets(c_len, 21, stdin), &endptr, 10, true, &len); 402 403 if ((rc = get_input(in, &str)) != EOK) 404 return rc; 405 406 if(str_uint32_t(str, &endptr, 10, true, &ea) != EOK || ea < sa) { 407 printf("Invalid value. Canceled.\n"); 408 return EINVAL; 409 } 410 411 free(str); 412 413 p->type = type; 414 p->bootable = (c == 'y' || c == 'Y') ? true : false; 415 p->start_addr = sa; 416 p->length = ea - sa; 417 418 return EOK; 419 } 420 421 253 void fill_table_funcs(void) 254 { 255 switch(table.layout) { 256 case LYT_MBR: 257 table.add_part = add_mbr_part; 258 table.delete_part = delete_mbr_part; 259 table.print_parts = print_mbr_parts; 260 table.write_parts = write_mbr_parts; 261 break; 262 case LYT_GPT: 263 table.add_part = add_gpt_part; 264 table.delete_part = delete_gpt_part; 265 table.print_parts = print_gpt_parts; 266 table.write_parts = write_gpt_parts; 267 break; 268 default: 269 break; 270 } 271 } 272 273 void free_table(void) 274 { 275 switch(table.layout) { 276 case LYT_MBR: 277 mbr_free_partitions(table.data.mbr.parts); 278 mbr_free_mbr(table.data.mbr.mbr); 279 break; 280 case LYT_GPT: 281 gpt_free_partitions(table.data.gpt.parts); 282 gpt_free_gpt(table.data.gpt.gpt); 283 break; 284 default: 285 break; 286 } 287 } 288
Note:
See TracChangeset
for help on using the changeset viewer.