Changeset 271e24a in mainline
- Timestamp:
- 2013-03-24T00:12:25Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 30440ed
- Parents:
- ec50ac4a
- Files:
-
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
boot/Makefile.common
rec50ac4a r271e24a 166 166 $(USPACE_PATH)/app/dload/dload \ 167 167 $(USPACE_PATH)/app/edit/edit \ 168 $(USPACE_PATH)/app/hdisk/hdisk \ 168 169 $(USPACE_PATH)/app/inet/inet \ 169 170 $(USPACE_PATH)/app/kill/kill \ -
uspace/app/hdisk/Makefile
rec50ac4a r271e24a 32 32 $(LIBBLOCK_PREFIX)/libblock.a $(LIBCLUI_PREFIX)/libclui.a \ 33 33 $(LIBC_PREFIX)/libc.a 34 EXTRA_CFLAGS = -I$(LIBMBR_PREFIX) -I$(LIB BLOCK_PREFIX) -I$(LIBCLUI_PREFIX)34 EXTRA_CFLAGS = -I$(LIBMBR_PREFIX) -I$(LIBGPT_PREFIX) -I$(LIBBLOCK_PREFIX) -I$(LIBCLUI_PREFIX) 35 35 BINARY = hdisk 36 36 … … 38 38 hdisk.c \ 39 39 func_mbr.c \ 40 func_gpt.c 40 func_gpt.c \ 41 input.c 41 42 42 43 include $(USPACE_PREFIX)/Makefile.common -
uspace/app/hdisk/func_gpt.c
rec50ac4a r271e24a 33 33 */ 34 34 35 #include <stdio.h> 36 #include <errno.h> 37 #include <sys/types.h> 38 35 39 #include "func_gpt.h" 40 41 int add_gpt_part(tinput_t * in, union table_data * data) 42 { 43 int rc = EOK; 44 45 return rc; 46 } 47 48 int delete_gpt_part(tinput_t * in, union table_data * data) 49 { 50 int rc = EOK; 51 52 return rc; 53 } 54 55 int print_gpt_parts(union table_data * data) 56 { 57 int rc = EOK; 58 59 return rc; 60 } 61 62 int write_gpt_parts(service_id_t dev_handle, union table_data * data) 63 { 64 int rc = EOK; 65 66 return rc; 67 } 68 -
uspace/app/hdisk/func_gpt.h
rec50ac4a r271e24a 34 34 35 35 #ifndef __FUNC_GPT_H__ 36 #def __FUNC_GPT_H__36 #define __FUNC_GPT_H__ 37 37 38 int add_gpt_part(tinput_t * in, gpt_parts_t * parts); 39 int delete_gpt_part(tinput_t * in, gpt_parts_t * parts); 40 extern void print_gpt_partitions(gpt_parts_t * parts); 41 extern void write_gpt_parts(gpt_parts_t * parts, gpt_t * mbr, service_id_t dev_handle); 38 #include <loc.h> 39 #include <tinput.h> 40 #include <libgpt.h> 41 42 #include "common.h" 43 44 extern int add_gpt_part(tinput_t * in, union table_data * data); 45 extern int delete_gpt_part(tinput_t * in, union table_data * data); 46 extern int print_gpt_parts(union table_data * data); 47 extern int write_gpt_parts(service_id_t dev_handle, union table_data * data); 42 48 43 49 #endif -
uspace/app/hdisk/func_mbr.c
rec50ac4a r271e24a 33 33 */ 34 34 35 #include <stdio.h> 36 #include <errno.h> 37 #include <sys/types.h> 38 35 39 #include "func_mbr.h" 40 #include "input.h" 36 41 37 int add_mbr_part(tinput_t * in, mbr_parts_t * parts) 42 static int set_mbr_partition(tinput_t * in, mbr_part_t * p); 43 44 45 int add_mbr_part(tinput_t * in, union table_data * data) 38 46 { 39 part_t * part = mbr_alloc_partition(); 40 41 printf("Primary (p) or logical (l): "); 42 int input = getchar(); 43 44 switch(input) { 45 case 'p': 46 mbr_set_flag(parts, ST_LOGIC, false); 47 case 'l': 48 mbr_set_flag(parts, ST_LOGIC, false); 49 default: 50 printf("Invalid type. Cancelled."); 51 return EINVAL; 52 } 47 int rc; 48 49 mbr_part_t * part = mbr_alloc_partition(); 53 50 54 51 set_mbr_partition(in, part); 55 52 56 mbr_add_partition(parts, part); 53 rc = mbr_add_partition(data->mbr.parts, part); 54 if (rc != EOK) { 55 printf("Error adding partition.\n"); 56 } 57 58 59 return rc; 57 60 } 58 61 59 int delete_mbr_part(tinput_t * in, part_t * parts)62 int delete_mbr_part(tinput_t * in, union table_data * data) 60 63 { 61 char * str;62 64 int rc; 63 part_t * temp = NULL; 64 part_t * p = parts; 65 uint32_t i, input = 0; 66 char * endptr; 65 size_t idx; 67 66 68 67 printf("Number of the partition to delete (counted from 0): "); 68 idx = get_input_size_t(in); 69 69 70 if((rc = get_input(in, &str)) != EOK) 71 return rc; 72 73 //convert from string to base 10 number 74 if(str_uint32_t(str, &endptr, 10, true, &input) != EOK) { 75 printf("Invalid value. Canceled.\n"); 76 return EINVAL; 77 } 78 79 free(str); 80 81 rc = mbr_remove_partition(parts, input); 70 rc = mbr_remove_partition(data->mbr.parts, idx); 82 71 if(rc != EOK) { 83 72 printf("Error: something.\n"); … … 88 77 89 78 /** Print current partition scheme */ 90 void print_mbr_partitions(mbr_parts_t * parts)79 int print_mbr_parts(union table_data * data) 91 80 { 92 81 int num = 0; … … 95 84 printf("\t\tBootable:\tStart:\tEnd:\tLength:\tType:\n"); 96 85 97 mbr_part_foreach( parts, it) {86 mbr_part_foreach(data->mbr.parts, it) { 98 87 if (it->type == PT_UNUSED) 99 88 continue; 100 89 101 printf("\t P%d:\t", i);102 if ( p->bootable)90 printf("\t P%d:\t", num); 91 if (mbr_get_flag(it, ST_BOOT)) 103 92 printf("*"); 104 93 else 105 94 printf(" "); 106 95 107 printf("\t\t% llu\t%llu\t%llu\t%d\n", p->start_addr, p->start_addr + p->length, p->length, p->type);96 printf("\t\t%u\t%u\t%u\t%d\n", it->start_addr, it->start_addr + it->length, it->length, it->type); 108 97 109 98 ++num; 110 } */99 } 111 100 112 101 printf("%d partitions found.\n", num); 102 103 return EOK; 113 104 } 114 105 115 void write_mbr_parts(mbr_parts_t * parts, mbr_t * mbr, service_id_t dev_handle)106 int write_mbr_parts(service_id_t dev_handle, union table_data * data) 116 107 { 117 int rc = mbr_write_partitions( parts,mbr, dev_handle);108 int rc = mbr_write_partitions(data->mbr.parts, data->mbr.mbr, dev_handle); 118 109 if (rc != EOK) { 119 110 printf("Error occured during writing. (ERR: %d)\n", rc); 120 111 } 112 113 return rc; 121 114 } 115 116 117 118 static int set_mbr_partition(tinput_t * in, mbr_part_t * p) 119 { 120 int c; 121 uint8_t type; 122 123 printf("Primary (p) or logical (l): "); 124 c = getchar(); 125 126 switch(c) { 127 case 'p': 128 mbr_set_flag(p, ST_LOGIC, false); 129 case 'l': 130 mbr_set_flag(p, ST_LOGIC, true); 131 default: 132 printf("Invalid type. Cancelled."); 133 return EINVAL; 134 } 135 136 printf("Set type (0-255): "); 137 type = get_input_uint8(in); 138 139 ///TODO: there can only be one bootable partition; let's do it just like fdisk 140 printf("Bootable? (y/n): "); 141 c = getchar(); 142 if (c != 'y' && c != 'Y' && c != 'n' && c != 'N') { 143 printf("Invalid value. Cancelled."); 144 return EINVAL; 145 } 146 147 mbr_set_flag(p, ST_BOOT, (c == 'y' || c == 'Y') ? true : false); 148 149 150 uint32_t sa, ea; 151 152 printf("Set starting address (number): "); 153 sa = get_input_uint32(in); 154 155 printf("Set end addres (number): "); 156 ea = get_input_uint32(in); 157 158 if(ea < sa) { 159 printf("Invalid value. Canceled.\n"); 160 return EINVAL; 161 } 162 163 p->type = type; 164 p->start_addr = sa; 165 p->length = ea - sa; 166 167 return EOK; 168 } 169 170 171 172 173 174 -
uspace/app/hdisk/func_mbr.h
rec50ac4a r271e24a 34 34 35 35 #ifndef __FUNC_MBR_H__ 36 #def __FUNC_MBR_H__36 #define __FUNC_MBR_H__ 37 37 38 int add_mbr_part(tinput_t * in, mbr_parts_t * parts); 39 int delete_mbr_part(tinput_t * in, mbr_parts_t * parts); 40 extern void print_mbr_partitions(mbr_parts_t * parts); 41 extern void write_mbr_parts(mbr_parts_t * parts, mbr_t * mbr, service_id_t dev_handle); 38 #include <loc.h> 39 #include <tinput.h> 40 #include <libmbr.h> 41 42 #include "common.h" 43 44 extern int add_mbr_part(tinput_t * in, union table_data * data); 45 extern int delete_mbr_part(tinput_t * in, union table_data * data); 46 extern int print_mbr_parts(union table_data * data); 47 extern int write_mbr_parts(service_id_t dev_handle, union table_data * data); 42 48 43 49 #endif -
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 -
uspace/lib/gpt/Makefile
rec50ac4a r271e24a 32 32 33 33 SOURCES = \ 34 libgpt.c 34 libgpt.c \ 35 global.c 35 36 36 37 include $(USPACE_PREFIX)/Makefile.common -
uspace/lib/gpt/libgpt.c
rec50ac4a r271e24a 52 52 53 53 static int load_and_check_header(service_id_t handle, aoff64_t addr, size_t b_size, gpt_header_t * header); 54 static gpt_part s_t * alloc_part_array(uint32_t num);55 static int extend_part_array(gpt_part s_t * p);56 static int reduce_part_array(gpt_part s_t * p);54 static gpt_partitions_t * alloc_part_array(uint32_t num); 55 static int extend_part_array(gpt_partitions_t * p); 56 static int reduce_part_array(gpt_partitions_t * p); 57 57 static long long nearest_larger_int(double a); 58 58 … … 162 162 * error code is stored in errno 163 163 */ 164 gpt_part s_t * gpt_read_partitions(gpt_t * gpt)164 gpt_partitions_t * gpt_read_partitions(gpt_t * gpt) 165 165 { 166 166 int rc; 167 167 unsigned int i; 168 gpt_part s_t * res;168 gpt_partitions_t * res; 169 169 uint32_t num_ent = uint32_t_le2host(gpt->raw_data->num_entries); 170 170 uint32_t ent_size = uint32_t_le2host(gpt->raw_data->entry_size); … … 242 242 * @return returns EOK on succes, specific error code otherwise 243 243 */ 244 int gpt_write_partitions(gpt_part s_t * parts, gpt_t * gpt, service_id_t dev_handle)244 int gpt_write_partitions(gpt_partitions_t * parts, gpt_t * gpt, service_id_t dev_handle) 245 245 { 246 246 int rc; … … 283 283 } 284 284 285 gpt_parts_t * gpt_add_partition(gpt_parts_t * parts, g_part_t * partition) 286 { 287 288 } 289 290 gpt_parts_t * gpt_remove_partition(gpt_parts_t * parts, int idx) 291 { 292 285 gpt_partitions_t * gpt_add_partition(gpt_partitions_t * parts, gpt_part_t * partition) 286 { 287 288 extend_part_array(parts); 289 return parts; 290 } 291 292 gpt_partitions_t * gpt_remove_partition(gpt_partitions_t * parts, size_t idx) 293 { 294 reduce_part_array(parts); 295 return parts; 293 296 } 294 297 … … 304 307 * @param parts partition list to be freed 305 308 */ 306 void gpt_free_partitions(gpt_part s_t * parts)309 void gpt_free_partitions(gpt_partitions_t * parts) 307 310 { 308 311 free(parts->part_array); … … 316 319 * 317 320 */ 318 void gpt_set_part_type(g _part_t * p, int type)321 void gpt_set_part_type(gpt_part_t * p, int type) 319 322 { 320 323 /* Beware: first 3 blocks are byteswapped! */ … … 386 389 } 387 390 388 static gpt_part s_t * alloc_part_array(uint32_t num)389 { 390 gpt_part s_t * res = malloc(sizeof(gpt_parts_t));391 static gpt_partitions_t * alloc_part_array(uint32_t num) 392 { 393 gpt_partitions_t * res = malloc(sizeof(gpt_partitions_t)); 391 394 if (res == NULL) { 392 395 errno = ENOMEM; … … 408 411 } 409 412 410 static int extend_part_array(gpt_part s_t * p)413 static int extend_part_array(gpt_partitions_t * p) 411 414 { 412 415 unsigned int nsize = p->arr_size * 2; … … 425 428 } 426 429 427 static int reduce_part_array(gpt_part s_t * p)430 static int reduce_part_array(gpt_partitions_t * p) 428 431 { 429 432 if(p->arr_size > GPT_MIN_PART_NUM) { -
uspace/lib/gpt/libgpt.h
rec50ac4a r271e24a 36 36 #define __GPT_H__ 37 37 38 #define NAME "libgpt"38 #define LIBGPT_NAME "libgpt" 39 39 40 #include <loc.h> 40 41 #include <sys/types.h> 41 42 … … 50 51 51 52 /** GPT header signature ("EFI PART" in ASCII) */ 52 const uint8_t efi_signature[8] = { 53 0x45, 0x46, 0x49, 0x20, 0x50, 0x41, 0x52, 0x54 54 }; 53 extern const uint8_t efi_signature[8]; 55 54 56 55 /** GPT header … … 102 101 /** Raw data access */ 103 102 gpt_entry_t raw_data; //TODO: a pointer or just a member? 104 }g _part_t;103 }gpt_part_t; 105 104 106 105 typedef struct gpt_parts { … … 111 110 /** Resizable partition array */ 112 111 gpt_entry_t * part_array; 113 } gpt_parts_t; 112 } gpt_partitions_t; 113 114 115 typedef struct gpt_table { 116 gpt_t * gpt; 117 gpt_partitions_t * parts; 118 } gpt_table_t; 114 119 115 120 struct partition_type { … … 118 123 }; 119 124 120 struct partition_type gpt_ptypes[] = { 121 { "Unused entry", "00000000-0000-0000-0000-000000000000" }, 122 { "MBR partition scheme", "024DEE41-33E7-11D3-9D69-0008C781F39F" }, 123 { "EFI System", "C12A7328-F81F-11D2-BA4B-00A0C93EC93B" }, 124 { "BIOS Boot", "21686148-6449-6E6F-744E-656564454649" }, 125 { "Windows Reserved", "E3C9E316-0B5C-4DB8-817D-F92DF00215AE" }, 126 { "Windows Basic data", "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7" }, 127 { "Windows LDM metadata", "5808C8AA-7E8F-42E0-85D2-E1E90434CFB3" }, 128 { "Windows LDM data", "AF9B60A0-1431-4F62-BC68-3311714A69AD" }, 129 { "Windows Recovery Environment", "DE94BBA4-06D1-4D40-A16A-BFD50179D6AC" }, 130 { "Windows IBM GPFS", "37AFFC90-EF7D-4E96-91C3-2D7AE055B174" }, 131 { "Windows Cluster metadata", "DB97DBA9-0840-4BAE-97F0-FFB9A327C7E1" }, 132 { "HP-UX Data", "75894C1E-3AEB-11D3-B7C1-7B03A0000000" }, 133 { "HP-UX Service", "E2A1E728-32E3-11D6-A682-7B03A0000000" }, 134 { "Linux filesystem data", "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7" }, 135 { "Linux filesystem data", "0FC63DAF-8483-4772-8E79-3D69D8477DE4" }, 136 { "Linux RAID", "A19D880F-05FC-4D3B-A006-743F0F84911E" }, 137 { "Linux Swap", "0657FD6D-A4AB-43C4-84E5-0933C84B4F4F" }, 138 { "Linux LVM", "E6D6D379-F507-44C2-A23C-238F2A3DF928" }, 139 { "Linux Reserved", "8DA63339-0007-60C0-C436-083AC8230908" }, 140 { "FreeBSD Boot", "83BD6B9D-7F41-11DC-BE0B-001560B84F0F" }, 141 { "FreeBSD Data", "516E7CB4-6ECF-11D6-8FF8-00022D09712B" }, 142 { "FreeBSD Swap", "516E7CB5-6ECF-11D6-8FF8-00022D09712B" }, 143 { "FreeBSD UFS", "516E7CB6-6ECF-11D6-8FF8-00022D09712B" }, 144 { "FreeBSD Vinum VM", "516E7CB8-6ECF-11D6-8FF8-00022D09712B" }, 145 { "FreeBSD ZFS", "516E7CBA-6ECF-11D6-8FF8-00022D09712B" }, 146 { "Mac OS X HFS+", "48465300-0000-11AA-AA11-00306543ECAC" }, 147 { "Mac OS X UFS", "55465300-0000-11AA-AA11-00306543ECAC" }, 148 { "Mac OS X ZFS", "6A898CC3-1DD2-11B2-99A6-080020736631" }, 149 { "Mac OS X RAID", "52414944-0000-11AA-AA11-00306543ECAC" }, 150 { "Mac OS X RAID, offline", "52414944-5F4F-11AA-AA11-00306543ECAC" }, 151 { "Mac OS X Boot", "426F6F74-0000-11AA-AA11-00306543ECAC" }, 152 { "Mac OS X Label", "4C616265-6C00-11AA-AA11-00306543ECAC" }, 153 { "Mac OS X TV Recovery", "5265636F-7665-11AA-AA11-00306543ECAC" }, 154 { "Mac OS X Core Storage", "53746F72-6167-11AA-AA11-00306543ECAC" }, 155 { "Solaris Boot", "6A82CB45-1DD2-11B2-99A6-080020736631" }, 156 { "Solaris Root", "6A85CF4D-1DD2-11B2-99A6-080020736631" }, 157 { "Solaris Swap", "6A87C46F-1DD2-11B2-99A6-080020736631" }, 158 { "Solaris Backup", "6A8B642B-1DD2-11B2-99A6-080020736631" }, 159 { "Solaris /usr", "6A898CC3-1DD2-11B2-99A6-080020736631" }, 160 { "Solaris /var", "6A8EF2E9-1DD2-11B2-99A6-080020736631" }, 161 { "Solaris /home", "6A90BA39-1DD2-11B2-99A6-080020736631" }, 162 { "Solaris Alternate sector", "6A9283A5-1DD2-11B2-99A6-080020736631" }, 163 { "Solaris Reserved", "6A945A3B-1DD2-11B2-99A6-080020736631" }, 164 { "Solaris Reserved", "6A9630D1-1DD2-11B2-99A6-080020736631" }, 165 { "Solaris Reserved", "6A980767-1DD2-11B2-99A6-080020736631" }, 166 { "Solaris Reserved", "6A96237F-1DD2-11B2-99A6-080020736631" }, 167 { "Solaris Reserved", "6A8D2AC7-1DD2-11B2-99A6-080020736631" }, 168 { "NetBSD Swap", "49F48D32-B10E-11DC-B99B-0019D1879648" }, 169 { "NetBSD FFS", "49F48D5A-B10E-11DC-B99B-0019D1879648" }, 170 { "NetBSD LFS", "49F48D82-B10E-11DC-B99B-0019D1879648" }, 171 { "NetBSD RAID", "49F48DAA-B10E-11DC-B99B-0019D1879648" }, 172 { "NetBSD Concatenated", "2DB519C4-B10F-11DC-B99B-0019D1879648" }, 173 { "NetBSD Encrypted", "2DB519EC-B10F-11DC-B99B-0019D1879648" }, 174 { "ChromeOS ChromeOS kernel", "FE3A2A5D-4F32-41A7-B725-ACCC3285A309" }, 175 { "ChromeOS rootfs", "3CB8E202-3B7E-47DD-8A3C-7FF2A13CFCEC" }, 176 { "ChromeOS future use", "2E0A753D-9E48-43B0-8337-B15192CB1B5E" }, 177 { "MidnightBSD Boot", "85D5E45E-237C-11E1-B4B3-E89A8F7FC3A7" }, 178 { "MidnightBSD Data", "85D5E45A-237C-11E1-B4B3-E89A8F7FC3A7" }, 179 { "MidnightBSD Swap", "85D5E45B-237C-11E1-B4B3-E89A8F7FC3A7" }, 180 { "MidnightBSD UFS", "0394Ef8B-237E-11E1-B4B3-E89A8F7FC3A7" }, 181 { "MidnightBSD Vinum VM", "85D5E45C-237C-11E1-B4B3-E89A8F7FC3A7" }, 182 { "MidnightBSD ZFS", "85D5E45D-237C-11E1-B4B3-E89A8F7FC3A7" } 183 }; 125 extern const struct partition_type gpt_ptypes[]; 126 184 127 185 128 … … 187 130 extern int gpt_write_gpt_header(gpt_t * header, service_id_t dev_handle); 188 131 189 extern gpt_part s_t * gpt_read_partitions(gpt_t * gpt);190 extern int gpt_write_partitions(gpt_parts_t * parts, gpt_t * header, service_id_t dev_handle);191 extern int gpt_add_partition(gpt_parts_t * parts, g_part_t * partition);192 extern void gpt_remove_partition(gpt_parts_t * parts, int idx);193 extern void gpt_set_part_type(g_part_t * p, int type);194 extern void gpt_set_part_name(gpt_entry_t * p, char * name[], size_t length);132 extern gpt_partitions_t * gpt_read_partitions(gpt_t * gpt); 133 extern int gpt_write_partitions(gpt_partitions_t * parts, gpt_t * header, service_id_t dev_handle); 134 extern gpt_partitions_t * gpt_add_partition(gpt_partitions_t * parts, gpt_part_t * partition); 135 extern gpt_partitions_t * gpt_remove_partition(gpt_partitions_t * parts, size_t idx); 136 extern void gpt_set_part_type(gpt_part_t * p, int type); 137 extern void gpt_set_part_name(gpt_entry_t * p, char * name[], size_t length); 195 138 196 139 extern void gpt_free_gpt(gpt_t * gpt); 197 extern void gpt_free_partitions(gpt_part s_t * parts);140 extern void gpt_free_partitions(gpt_partitions_t * parts); 198 141 199 142 #endif 143 -
uspace/lib/mbr/libmbr.c
rec50ac4a r271e24a 12 12 * notice, this list of conditions and the following disclaimer in the 13 13 * documentation and/or other materials provided with the distribution. 14 * - The nameof the author may not be used to endorse or promote products14 * - The LIBMBR_NAME of the author may not be used to endorse or promote products 15 15 * derived from this software without specific prior written permission. 16 16 * … … 45 45 46 46 static br_block_t * alloc_br(void); 47 static int decode_part(pt_entry_t * src, part_t * trgt, uint32_t base);48 static int decode_logical(mbr_t * mbr, mbr_part s_t * p,part_t * ext);49 static void encode_part( part_t * src, pt_entry_t * trgt, uint32_t base);47 static int decode_part(pt_entry_t * src, mbr_part_t * trgt, uint32_t base); 48 static int decode_logical(mbr_t * mbr, mbr_partitions_t * p, mbr_part_t * ext); 49 static void encode_part(mbr_part_t * src, pt_entry_t * trgt, uint32_t base); 50 50 51 51 /** Read MBR from specific device … … 123 123 * @return linked list of partitions or NULL on error 124 124 */ 125 mbr_part s_t * mbr_read_partitions(mbr_t * mbr)125 mbr_partitions_t * mbr_read_partitions(mbr_t * mbr) 126 126 { 127 127 int rc, i; 128 part_t * p;129 part_t * ext = NULL;130 mbr_part s_t * parts;128 mbr_part_t * p; 129 mbr_part_t * ext = NULL; 130 mbr_partitions_t * parts; 131 131 132 132 if (mbr == NULL) … … 143 143 continue; 144 144 145 p = malloc(sizeof( part_t));145 p = malloc(sizeof(mbr_part_t)); 146 146 if (p == NULL) { 147 printf( NAME ": Error on memory allocation.\n");147 printf(LIBMBR_NAME ": Error on memory allocation.\n"); 148 148 free(p); 149 149 mbr_free_partitions(parts); … … 159 159 rc = decode_logical(mbr, parts, ext); 160 160 if (rc != EOK) { 161 printf( NAME ": Error occured during decoding the MBR.\n" \162 NAME ": Partition list may be incomplete.\n");161 printf(LIBMBR_NAME ": Error occured during decoding the MBR.\n" \ 162 LIBMBR_NAME ": Partition list may be incomplete.\n"); 163 163 } 164 164 … … 173 173 * @return returns EOK on succes, specific error code otherwise 174 174 */ 175 int mbr_write_partitions(mbr_part s_t * parts, mbr_t * mbr, service_id_t dev_handle)175 int mbr_write_partitions(mbr_partitions_t * parts, mbr_t * mbr, service_id_t dev_handle) 176 176 { 177 177 bool logical = false; 178 178 int i = 0; 179 179 int rc; 180 part_t * p;181 part_t * ext = (parts->l_extended == NULL) ? NULL182 : list_get_instance(parts->l_extended, part_t, link);180 mbr_part_t * p; 181 mbr_part_t * ext = (parts->l_extended == NULL) ? NULL 182 : list_get_instance(parts->l_extended, mbr_part_t, link); 183 183 184 184 br_block_t * last_ebr = NULL; … … 194 194 195 195 aoff64_t addr = ext->start_addr; 196 part_t * prev_part = NULL;196 mbr_part_t * prev_part = NULL; 197 197 198 198 list_foreach(parts->list, it) { 199 p = list_get_instance(it, part_t, link);199 p = list_get_instance(it, mbr_part_t, link); 200 200 if (mbr_get_flag(p, ST_LOGIC)) { 201 201 // writing logical partition … … 269 269 270 270 list_foreach(parts->list, it) { 271 p = list_get_instance(it, part_t, link);271 p = list_get_instance(it, mbr_part_t, link); 272 272 if (mbr_get_flag(p, ST_LOGIC)) { 273 273 // extended does not exist, fail … … 295 295 ext = p; 296 296 297 //p = list_get_instance(p->link.next, mbr_part s_t, link);297 //p = list_get_instance(p->link.next, mbr_partitions_t, link); 298 298 p = p->next; 299 299 } … … 357 357 } 358 358 359 /** part_t constructor */360 part_t * mbr_alloc_partition(void)361 { 362 part_t * p = malloc(sizeof(part_t));359 /** mbr_part_t constructor */ 360 mbr_part_t * mbr_alloc_partition(void) 361 { 362 mbr_part_t * p = malloc(sizeof(mbr_part_t)); 363 363 if (p == NULL) { 364 364 return NULL; … … 374 374 } 375 375 376 mbr_part s_t * mbr_alloc_partitions(void)377 { 378 mbr_part s_t * parts = malloc(sizeof(mbr_parts_t));376 mbr_partitions_t * mbr_alloc_partitions(void) 377 { 378 mbr_partitions_t * parts = malloc(sizeof(mbr_partitions_t)); 379 379 if (parts == NULL) { 380 380 return NULL; … … 387 387 388 388 /** Add partition */ 389 void mbr_add_partition(mbr_parts_t * parts,part_t * partition)389 int mbr_add_partition(mbr_partitions_t * parts, mbr_part_t * partition) 390 390 { 391 391 list_append(&(partition->link), &(parts->list)); 392 return EOK; 392 393 } 393 394 394 395 /** Remove partition */ 395 void mbr_remove_partition(mbr_parts_t * parts, int idx)396 int mbr_remove_partition(mbr_partitions_t * parts, size_t idx) 396 397 { 397 398 link_t * l = list_nth(&(parts->list), idx); 398 399 list_remove(l); 399 part_t * p = list_get_instance(l,part_t, link);400 mbr_part_t * p = list_get_instance(l, mbr_part_t, link); 400 401 mbr_free_partition(p); 401 } 402 403 /** part_t destructor */ 404 void mbr_free_partition(part_t * p) 402 403 return EOK; 404 } 405 406 /** mbr_part_t destructor */ 407 void mbr_free_partition(mbr_part_t * p) 405 408 { 406 409 if (p->ebr != NULL) … … 410 413 411 414 /** Get flag bool value */ 412 int mbr_get_flag( part_t * p, MBR_FLAGS flag)415 int mbr_get_flag(mbr_part_t * p, MBR_FLAGS flag) 413 416 { 414 417 return (p->status & (1 << flag)) ? 1 : 0; … … 416 419 417 420 /** Set a specifig status flag to a value */ 418 void mbr_set_flag( part_t * p, MBR_FLAGS flag, bool value)421 void mbr_set_flag(mbr_part_t * p, MBR_FLAGS flag, bool value) 419 422 { 420 423 uint8_t status = p->status; … … 438 441 * @param parts partition list to be freed 439 442 */ 440 void mbr_free_partitions(mbr_part s_t * parts)443 void mbr_free_partitions(mbr_partitions_t * parts) 441 444 { 442 445 list_foreach_safe(parts->list, cur_link, next) { 443 part_t * p = list_get_instance(cur_link,part_t, link);446 mbr_part_t * p = list_get_instance(cur_link, mbr_part_t, link); 444 447 list_remove(cur_link); 445 448 mbr_free_partition(p); … … 462 465 } 463 466 464 /** Parse partition entry to part_t */465 static int decode_part(pt_entry_t * src, part_t * trgt, uint32_t base)467 /** Parse partition entry to mbr_part_t */ 468 static int decode_part(pt_entry_t * src, mbr_part_t * trgt, uint32_t base) 466 469 { 467 470 trgt->type = src->ptype; … … 477 480 } 478 481 479 /** Parse MBR contents to part_t list482 /** Parse MBR contents to mbr_part_t list 480 483 * parameter 'p' is allocated for only used primary partitions 481 484 */ 482 static int decode_logical(mbr_t * mbr, mbr_part s_t * parts,part_t * ext)485 static int decode_logical(mbr_t * mbr, mbr_partitions_t * parts, mbr_part_t * ext) 483 486 { 484 487 int rc; 485 part_t * p;488 mbr_part_t * p; 486 489 487 490 if (mbr == NULL || parts == NULL) … … 534 537 } 535 538 536 /** Convert part_t to pt_entry_t */537 static void encode_part( part_t * src, pt_entry_t * trgt, uint32_t base)539 /** Convert mbr_part_t to pt_entry_t */ 540 static void encode_part(mbr_part_t * src, pt_entry_t * trgt, uint32_t base) 538 541 { 539 542 if (src != NULL) { -
uspace/lib/mbr/libmbr.h
rec50ac4a r271e24a 36 36 #define LIBMBR_LIBMBR_H_ 37 37 38 #define NAME "libmbr" 38 #include <sys/types.h> 39 40 #define LIBMBR_NAME "libmbr" 39 41 40 42 /** Number of primary partition records */ … … 108 110 109 111 110 //FIXME: make mbr_part s_t as the linked list for keeping the same interface as with GPT112 //FIXME: make mbr_partitions_t as the linked list for keeping the same interface as with GPT 111 113 /** Partition */ 112 typedef struct part {114 typedef struct mbr_part { 113 115 /** The link in the doubly-linked list */ 114 116 link_t link; … … 118 120 uint8_t status; 119 121 /** Address of first block */ 120 aoff64_t start_addr;122 uint32_t start_addr; 121 123 /** Number of blocks */ 122 aoff64_t length;124 uint32_t length; 123 125 /** Points to Extended Boot Record of logical partition */ 124 126 br_block_t * ebr; 125 } part_t;127 } mbr_part_t; 126 128 127 129 typedef struct mbr_parts { … … 134 136 /** Partition linked list */ 135 137 list_t list; 136 } mbr_part s_t;138 } mbr_partitions_t; 137 139 140 typedef struct mbr_table { 141 mbr_t * mbr; 142 mbr_partitions_t * parts; 143 } mbr_table_t; 138 144 139 145 /** Read/Write MBR header. … … 146 152 147 153 /** Read/Write/Set MBR partitions. */ 148 extern mbr_part s_t * mbr_read_partitions(mbr_t * mbr);149 extern int mbr_write_partitions(mbr_parts_t * parts, mbr_t * mbr, service_id_t dev_handle);150 extern part_t *mbr_alloc_partition(void);151 extern mbr_part s_t * mbr_alloc_partitions(void);152 extern void mbr_add_partition(mbr_parts_t * parts,part_t * partition);153 extern void mbr_remove_partition(mbr_parts_t * parts, int idx);154 extern int mbr_get_flag(part_t * p, MBR_FLAGS flag);155 extern void mbr_set_flag(part_t * p, MBR_FLAGS flag, bool value);154 extern mbr_partitions_t * mbr_read_partitions(mbr_t * mbr); 155 extern int mbr_write_partitions(mbr_partitions_t * parts, mbr_t * mbr, service_id_t dev_handle); 156 extern mbr_part_t * mbr_alloc_partition(void); 157 extern mbr_partitions_t * mbr_alloc_partitions(void); 158 extern int mbr_add_partition(mbr_partitions_t * parts, mbr_part_t * partition); 159 extern int mbr_remove_partition(mbr_partitions_t * parts, size_t idx); 160 extern int mbr_get_flag(mbr_part_t * p, MBR_FLAGS flag); 161 extern void mbr_set_flag(mbr_part_t * p, MBR_FLAGS flag, bool value); 156 162 157 163 #define mbr_part_foreach(parts, iterator) \ 158 list_foreach(parts->list, iterator) 164 for (mbr_part_t * iterator = list_get_instance((parts)->list.head.next, mbr_part_t, link); \ 165 iterator != list_get_instance(&(parts)->list.head, mbr_part_t, link); \ 166 iterator = list_get_instance(iterator->link.next, mbr_part_t, link)) 167 159 168 160 169 /** free() wrapper functions. */ 161 170 extern void mbr_free_mbr(mbr_t * mbr); 162 extern void mbr_free_partition( part_t * p);163 extern void mbr_free_partitions(mbr_part s_t * parts);171 extern void mbr_free_partition(mbr_part_t * p); 172 extern void mbr_free_partitions(mbr_partitions_t * parts); 164 173 165 174 #endif
Note:
See TracChangeset
for help on using the changeset viewer.