Changeset 6453e306 in mainline for uspace/app/hdisk/func_mbr.c
- Timestamp:
- 2013-12-25T16:09:43Z (11 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- ac36aed
- Parents:
- d51beba3
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/hdisk/func_mbr.c
rd51beba3 r6453e306 1 1 /* 2 * Copyright (c) 2012 ,2013 Dominik Taborsky2 * Copyright (c) 2012-2013 Dominik Taborsky 3 3 * All rights reserved. 4 4 * … … 27 27 */ 28 28 29 29 /** @addtogroup hdisk 30 30 * @{ 31 31 */ … … 37 37 #include <str_error.h> 38 38 #include <sys/types.h> 39 40 39 #include "func_mbr.h" 41 40 #include "input.h" … … 62 61 int add_mbr_part(label_t *this, tinput_t *in) 63 62 { 64 int rc;65 66 mbr_part_t *part = mbr_alloc_partition();67 68 rc = set_mbr_partition(in, part, this);63 mbr_part_t *partition = mbr_alloc_partition(); 64 if (partition == NULL) 65 return ENOMEM; 66 67 int rc = set_mbr_partition(in, partition, this); 69 68 if (rc != EOK) 70 69 return rc; 71 70 72 rc = mbr_add_partition(this->data.mbr, part );73 if (rc != ERR_OK) {71 rc = mbr_add_partition(this->data.mbr, partition); 72 if (rc != ERR_OK) 74 73 printf("Error adding partition: %d\n", rc); 75 }76 74 77 75 return EOK; … … 80 78 int delete_mbr_part(label_t *this, tinput_t *in) 81 79 { 82 int rc; 83 size_t idx; 84 85 printf("Number of the partition to delete (counted from 0): "); 86 idx = get_input_size_t(in); 87 88 if (idx == 0 && errno != EOK) 89 return errno; 90 91 rc = mbr_remove_partition(this->data.mbr, idx); 92 if (rc != EOK) { 80 printf("Index of the partition to delete (counted from 0): "); 81 size_t idx = get_input_size_t(in); 82 83 if ((idx == 0) && (errno != EOK)) 84 return errno; 85 86 int rc = mbr_remove_partition(this->data.mbr, idx); 87 if (rc != EOK) 93 88 printf("Error: partition does not exist?\n"); 94 }95 89 96 90 return EOK; … … 113 107 } 114 108 115 /** Print current partition scheme */116 109 int print_mbr_parts(label_t *this) 117 110 { 118 int num = 0; 119 120 printf("Current partition scheme (MBR)(number of blocks: %" PRIu64 "):\n", this->nblocks); 121 printf("\t\t%10s %10s %10s %10s %7s\n", "Bootable:", "Start:", "End:", "Length:", "Type:"); 122 111 printf("Current partition scheme: MBR\n"); 112 printf("Number of blocks: %" PRIu64 "\n", this->blocks); 113 printf("\t\t%10s %10s %10s %10s %7s\n", 114 "Bootable:", "Start:", "End:", "Length:", "Type:"); 115 116 unsigned int num = 0; 123 117 mbr_part_t *it; 124 125 118 for (it = mbr_get_first_partition(this->data.mbr); it != NULL; 126 119 it = mbr_get_next_partition(this->data.mbr, it)) { … … 128 121 continue; 129 122 130 printf("\tP% d:\t", num);123 printf("\tP%u:\t", num); 131 124 if (mbr_get_flag(it, ST_BOOT)) 132 125 printf("*"); … … 134 127 printf(" "); 135 128 136 printf("\t%10u %10u %10u %7u\n", it->start_addr, it->start_addr + it->length, it->length, it->type); 129 printf("\t%10u %10u %10u %7u\n", it->start_addr, 130 it->start_addr + it->length, it->length, it->type); 137 131 138 132 num++; 139 133 } 140 134 141 printf("% dpartitions found.\n", num);135 printf("%u partitions found.\n", num); 142 136 143 137 return EOK; … … 146 140 int read_mbr_parts(label_t *this) 147 141 { 148 int rc; 149 rc = mbr_read_mbr(this->data.mbr, this->device); 142 int rc = mbr_read_mbr(this->data.mbr, this->device); 150 143 if (rc != EOK) 151 144 return rc; … … 164 157 { 165 158 int rc = mbr_write_partitions(this->data.mbr, this->device); 166 if (rc != EOK) {167 printf("Error occured during writing: ERR: %d: %s\n", rc, str_error(rc));168 }159 if (rc != EOK) 160 printf("Error occured during writing: ERR: %d: %s\n", rc, 161 str_error(rc)); 169 162 170 163 return rc; … … 177 170 } 178 171 179 static int set_mbr_partition(tinput_t *in, mbr_part_t *p, label_t * this) 180 { 181 int c; 182 uint8_t type; 183 172 static int set_mbr_partition(tinput_t *in, mbr_part_t *partition, label_t *this) 173 { 184 174 printf("Primary (p) or logical (l): "); 185 c = getchar();175 int c = getchar(); 186 176 printf("%c\n", c); 187 177 188 178 switch (c) { 189 179 case 'p': 190 mbr_set_flag(p , ST_LOGIC, false);180 mbr_set_flag(partition, ST_LOGIC, false); 191 181 break; 192 182 case 'l': 193 mbr_set_flag(p , ST_LOGIC, true);183 mbr_set_flag(partition, ST_LOGIC, true); 194 184 break; 195 185 default: … … 198 188 } 199 189 200 printf("Set type (0 -255): ");201 type = get_input_uint8(in);202 if ( type == 0 && errno != EOK)203 return errno; 204 205 // /TODO: there can only be one boolabel partition; let's do it just like fdisk190 printf("Set type (0 - 255): "); 191 uint8_t type = get_input_uint8(in); 192 if ((type == 0) && (errno != EOK)) 193 return errno; 194 195 // FIXME: Make sure there is at most one bootable partition 206 196 printf("Bootable? (y/n): "); 207 197 c = getchar(); 208 if ( c != 'y' && c != 'Y' && c != 'n' && c != 'N') {198 if ((c != 'y') && (c != 'Y') && (c != 'n') && (c != 'N')) { 209 199 printf("Invalid value. Cancelled."); 210 200 return EINVAL; 211 201 } 202 212 203 printf("%c\n", c); 213 mbr_set_flag(p, ST_BOOT, (c == 'y' || c == 'Y') ? true : false); 214 215 216 uint32_t sa, ea; 217 204 mbr_set_flag(partition, ST_BOOT, (c == 'y' || c == 'Y') ? true : false); 205 218 206 printf("Set starting address: "); 219 sa = get_input_uint32(in); 220 if (sa == 0 && errno != EOK) 221 return errno; 222 223 if (this->alignment != 0 && this->alignment != 1 && sa % this->alignment != 0) { 207 uint32_t sa = get_input_uint32(in); 208 if ((sa == 0) && (errno != EOK)) 209 return errno; 210 211 if ((this->alignment != 0) && (this->alignment != 1) && 212 (sa % this->alignment != 0)) { 224 213 sa = mbr_get_next_aligned(sa, this->alignment); 225 printf("Starting address was aligned to % u.\n", sa);226 } 227 228 printf("Set end addres (max: %" PRIu64 "): ", this-> nblocks);229 ea = get_input_uint32(in);230 if ( ea == 0 && errno != EOK)214 printf("Starting address was aligned to %" PRIu32 ".\n", sa); 215 } 216 217 printf("Set end addres (max: %" PRIu64 "): ", this->blocks); 218 uint32_t ea = get_input_uint32(in); 219 if ((ea == 0) && (errno != EOK)) 231 220 return errno; 232 221 … … 236 225 } 237 226 238 p->type = type; 239 p->start_addr = sa; 240 p->length = ea - sa; 241 242 return EOK; 243 } 244 245 246 247 248 249 227 partition->type = type; 228 partition->start_addr = sa; 229 partition->length = ea - sa; 230 231 return EOK; 232 }
Note:
See TracChangeset
for help on using the changeset viewer.