Changes in / [c9f61150:9bda5d90] in mainline
- Files:
-
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/toolchain.sh
rc9f61150 r9bda5d90 53 53 EOF 54 54 55 BINUTILS_VERSION="2.2 3.1"55 BINUTILS_VERSION="2.22" 56 56 BINUTILS_RELEASE="" 57 GCC_VERSION="4.8.0" 58 GDB_VERSION="7.5.1" 57 #GCC_VERSION="4.7.2" 58 GCC_VERSION="4.7.3" 59 #GCC_VERSION="4.8.0" 60 GDB_VERSION="7.5" 59 61 60 62 BASEDIR="`pwd`" … … 71 73 HEADER="$2" 72 74 BODY="$3" 73 75 74 76 FNAME="/tmp/conftest-$$" 75 77 76 78 echo "#include ${HEADER}" > "${FNAME}.c" 77 79 echo >> "${FNAME}.c" … … 81 83 echo " return 0;" >> "${FNAME}.c" 82 84 echo "}" >> "${FNAME}.c" 83 85 84 86 cc -c -o "${FNAME}.o" "${FNAME}.c" 2> "${FNAME}.log" 85 87 RC="$?" 86 88 87 89 if [ "$RC" -ne "0" ] ; then 88 90 echo " ${DEPENDENCY} not found, too old or compiler error." … … 113 115 echo 114 116 echo "Script failed: $2" 115 117 116 118 exit 1 117 119 fi … … 121 123 FILE="$1" 122 124 SUM="$2" 123 125 124 126 COMPUTED="`md5sum "${FILE}" | cut -d' ' -f1`" 125 127 if [ "${SUM}" != "${COMPUTED}" ] ; then 126 128 echo 127 129 echo "Checksum of ${FILE} does not match." 128 130 129 131 exit 2 130 132 fi … … 156 158 echo "defined, /usr/local/cross will be used by default." 157 159 echo 158 160 159 161 exit 3 160 162 } … … 166 168 show_countdown() { 167 169 TM="$1" 168 170 169 171 if [ "${TM}" -eq 0 ] ; then 170 172 echo 171 173 return 0 172 174 fi 173 175 174 176 echo -n "${TM} " 175 177 change_title "${TM}" 176 178 sleep 1 177 179 178 180 TM="`expr "${TM}" - 1`" 179 181 show_countdown "${TM}" … … 211 213 FILE="$2" 212 214 CHECKSUM="$3" 213 215 214 216 if [ ! -f "${FILE}" ]; then 215 217 change_title "Downloading ${FILE}" … … 217 219 check_error $? "Error downloading ${FILE}." 218 220 fi 219 221 220 222 check_md5 "${FILE}" "${CHECKSUM}" 221 223 } … … 223 225 source_check() { 224 226 FILE="$1" 225 227 226 228 if [ ! -f "${FILE}" ]; then 227 229 echo 228 230 echo "File ${FILE} not found." 229 231 230 232 exit 4 231 233 fi … … 234 236 cleanup_dir() { 235 237 DIR="$1" 236 238 237 239 if [ -d "${DIR}" ]; then 238 240 change_title "Removing ${DIR}" … … 245 247 DIR="$1" 246 248 DESC="$2" 247 249 248 250 change_title "Creating ${DESC}" 249 251 echo ">>> Creating ${DESC}" 250 252 251 253 mkdir -p "${DIR}" 252 254 test -d "${DIR}" … … 257 259 FILE="$1" 258 260 DESC="$2" 259 261 260 262 change_title "Unpacking ${DESC}" 261 263 echo " >>> Unpacking ${DESC}" 262 264 263 265 tar -xjf "${FILE}" 264 266 check_error $? "Error unpacking ${DESC}." … … 269 271 check_dependecies 270 272 show_countdown 10 271 273 272 274 BINUTILS_SOURCE="ftp://ftp.gnu.org/gnu/binutils/" 273 275 GCC_SOURCE="ftp://ftp.gnu.org/gnu/gcc/gcc-${GCC_VERSION}/" 274 276 GDB_SOURCE="ftp://ftp.gnu.org/gnu/gdb/" 275 276 download_fetch "${BINUTILS_SOURCE}" "${BINUTILS}" " 33adb18c3048d057ac58d07a3f1adb38"277 download_fetch "${GCC_SOURCE}" "${GCC}" " e6040024eb9e761c3bea348d1fa5abb0"278 download_fetch "${GDB_SOURCE}" "${GDB}" " 3f48f468b24447cf24820054ff6e85b1"277 278 download_fetch "${BINUTILS_SOURCE}" "${BINUTILS}" "ee0f10756c84979622b992a4a61ea3f5" 279 download_fetch "${GCC_SOURCE}" "${GCC}" "cc308a0891e778cfda7a151ab8a6e762" 280 download_fetch "${GDB_SOURCE}" "${GDB}" "24a6779a9fe0260667710de1b082ef61" 279 281 } 280 282 … … 282 284 PLATFORM="$1" 283 285 TARGET="$2" 284 286 285 287 WORKDIR="${BASEDIR}/${PLATFORM}" 286 288 BINUTILSDIR="${WORKDIR}/binutils-${BINUTILS_VERSION}" … … 288 290 OBJDIR="${WORKDIR}/gcc-obj" 289 291 GDBDIR="${WORKDIR}/gdb-${GDB_VERSION}" 290 292 291 293 if [ -z "${CROSS_PREFIX}" ] ; then 292 294 CROSS_PREFIX="/usr/local/cross" 293 295 fi 294 296 295 297 PREFIX="${CROSS_PREFIX}/${PLATFORM}" 296 298 297 299 echo ">>> Downloading tarballs" 298 source_check "${BASEDIR}/${BINUTILS}"300 #source_check "${BASEDIR}/${BINUTILS}" 299 301 source_check "${BASEDIR}/${GCC}" 300 source_check "${BASEDIR}/${GDB}"301 302 #source_check "${BASEDIR}/${GDB}" 303 302 304 echo ">>> Removing previous content" 303 cleanup_dir "${PREFIX}"304 cleanup_dir "${WORKDIR}"305 305 #cleanup_dir "${PREFIX}" 306 #cleanup_dir "${WORKDIR}" 307 306 308 create_dir "${PREFIX}" "destination directory" 307 309 create_dir "${OBJDIR}" "GCC object directory" 308 310 309 311 echo ">>> Unpacking tarballs" 310 312 cd "${WORKDIR}" 311 313 check_error $? "Change directory failed." 312 313 unpack_tarball "${BASEDIR}/${BINUTILS}" "binutils"314 315 #unpack_tarball "${BASEDIR}/${BINUTILS}" "binutils" 314 316 unpack_tarball "${BASEDIR}/${GCC}" "GCC" 315 unpack_tarball "${BASEDIR}/${GDB}" "GDB"316 317 echo ">>> Processing binutils (${PLATFORM})"318 cd "${BINUTILSDIR}"319 check_error $? "Change directory failed."320 321 change_title "binutils: configure (${PLATFORM})"322 CFLAGS=-Wno-error ./configure "--target=${TARGET}" "--prefix=${PREFIX}" "--program-prefix=${TARGET}-" --disable-nls --disable-werror323 check_error $? "Error configuring binutils."324 325 change_title "binutils: make (${PLATFORM})"326 make all install327 check_error $? "Error compiling/installing binutils."328 317 #unpack_tarball "${BASEDIR}/${GDB}" "GDB" 318 319 #echo ">>> Processing binutils (${PLATFORM})" 320 #cd "${BINUTILSDIR}" 321 #check_error $? "Change directory failed." 322 323 #change_title "binutils: configure (${PLATFORM})" 324 #CFLAGS=-Wno-error ./configure "--target=${TARGET}" "--prefix=${PREFIX}" "--program-prefix=${TARGET}-" --disable-nls --disable-werror 325 #check_error $? "Error configuring binutils." 326 327 #change_title "binutils: make (${PLATFORM})" 328 #make all install 329 #check_error $? "Error compiling/installing binutils." 330 329 331 echo ">>> Processing GCC (${PLATFORM})" 330 332 cd "${OBJDIR}" 331 333 check_error $? "Change directory failed." 332 334 333 335 change_title "GCC: configure (${PLATFORM})" 334 336 "${GCCDIR}/configure" "--target=${TARGET}" "--prefix=${PREFIX}" "--program-prefix=${TARGET}-" --with-gnu-as --with-gnu-ld --disable-nls --disable-threads --enable-languages=c,objc,c++,obj-c++ --disable-multilib --disable-libgcj --without-headers --disable-shared --enable-lto --disable-werror 335 337 check_error $? "Error configuring GCC." 336 338 337 339 change_title "GCC: make (${PLATFORM})" 338 340 PATH="${PATH}:${PREFIX}/bin" make all-gcc install-gcc 339 341 check_error $? "Error compiling/installing GCC." 340 341 echo ">>> Processing GDB (${PLATFORM})"342 cd "${GDBDIR}"343 check_error $? "Change directory failed."344 345 change_title "GDB: configure (${PLATFORM})"346 ./configure "--target=${TARGET}" "--prefix=${PREFIX}" "--program-prefix=${TARGET}-"347 check_error $? "Error configuring GDB."348 349 change_title "GDB: make (${PLATFORM})"350 make all install351 check_error $? "Error compiling/installing GDB."352 342 343 #echo ">>> Processing GDB (${PLATFORM})" 344 #cd "${GDBDIR}" 345 #check_error $? "Change directory failed." 346 347 #change_title "GDB: configure (${PLATFORM})" 348 #./configure "--target=${TARGET}" "--prefix=${PREFIX}" "--program-prefix=${TARGET}-" 349 #check_error $? "Error configuring GDB." 350 351 #change_title "GDB: make (${PLATFORM})" 352 #make all install 353 #check_error $? "Error compiling/installing GDB." 354 353 355 cd "${BASEDIR}" 354 356 check_error $? "Change directory failed." 355 357 356 358 echo ">>> Cleaning up" 357 359 cleanup_dir "${WORKDIR}" 358 360 359 361 echo 360 362 echo ">>> Cross-compiler for ${TARGET} installed." … … 367 369 case "$1" in 368 370 "amd64") 369 prepare371 #prepare 370 372 build_target "amd64" "amd64-linux-gnu" 371 373 ;; … … 375 377 ;; 376 378 "ia32") 377 prepare379 #prepare 378 380 build_target "ia32" "i686-pc-linux-gnu" 379 381 ;; … … 438 440 build_target "arm32" "arm-linux-gnueabi" & 439 441 wait 440 442 441 443 build_target "ia32" "i686-pc-linux-gnu" & 442 444 build_target "ia64" "ia64-pc-linux-gnu" & 443 445 wait 444 446 445 447 build_target "mips32" "mipsel-linux-gnu" & 446 448 build_target "mips32eb" "mips-linux-gnu" & 447 449 wait 448 450 449 451 build_target "mips64" "mips64el-linux-gnu" & 450 452 build_target "ppc32" "ppc-linux-gnu" & 451 453 wait 452 454 453 455 build_target "ppc64" "ppc64-linux-gnu" & 454 456 build_target "sparc64" "sparc64-linux-gnu" & -
uspace/app/hdisk/common.h
rc9f61150 r9bda5d90 39 39 #include <libgpt.h> 40 40 41 union label_data {42 mbr_ label_t *mbr;43 gpt_ label_t *gpt;41 union table_data { 42 mbr_table_t mbr; 43 gpt_table_t gpt; 44 44 }; 45 45 -
uspace/app/hdisk/func_gpt.c
rc9f61150 r9bda5d90 43 43 static int set_gpt_partition(tinput_t *, gpt_part_t *); 44 44 45 int add_gpt_part(tinput_t * in, union label_data * data)45 int add_gpt_part(tinput_t * in, union table_data * data) 46 46 { 47 gpt_part_t * p = gpt_alloc_partition(data->gpt ->parts);47 gpt_part_t * p = gpt_alloc_partition(data->gpt.parts); 48 48 if (p == NULL) { 49 49 return ENOMEM; … … 53 53 } 54 54 55 int delete_gpt_part(tinput_t * in, union label_data * data)55 int delete_gpt_part(tinput_t * in, union table_data * data) 56 56 { 57 57 size_t idx; … … 60 60 idx = get_input_size_t(in); 61 61 62 if (gpt_remove_partition(data->gpt ->parts, idx) == -1) {62 if (gpt_remove_partition(data->gpt.parts, idx) == -1) { 63 63 printf("Warning: running low on memory, not resizing...\n"); 64 64 } … … 67 67 } 68 68 69 int destroy_gpt_label(union label_data *data)69 int new_gpt_table(tinput_t * in, union table_data * data) 70 70 { 71 data->gpt.gpt = gpt_alloc_gpt_header(); 72 data->gpt.parts = gpt_alloc_partitions(); 71 73 return EOK; 72 74 } 73 75 74 int new_gpt_label(union label_data *data) 75 { 76 data->gpt->gpt = gpt_alloc_gpt_header(); 77 data->gpt->parts = gpt_alloc_partitions(); 78 return EOK; 79 } 80 81 int print_gpt_parts(union label_data *data) 76 int print_gpt_parts(union table_data * data) 82 77 { 83 78 //int rc; … … 87 82 size_t i = 0; 88 83 89 gpt_part_foreach(data->gpt ->parts, iter) {84 gpt_part_foreach(data->gpt.parts, iter) { 90 85 //printf("\t%10u %10u %10u %3d\n", iter->start_addr, iter->start_addr + iter->length, 91 86 // iter->length, gpt_get_part_type(iter), gpt_get_part_name(iter)); … … 100 95 } 101 96 102 int read_gpt_parts(service_id_t dev_handle, union label_data *data) 103 { 104 return EOK; 105 } 106 107 int write_gpt_parts(service_id_t dev_handle, union label_data * data) 97 int write_gpt_parts(service_id_t dev_handle, union table_data * data) 108 98 { 109 99 int rc; 110 100 111 rc = gpt_write_partitions(data->gpt ->parts, data->gpt->gpt, dev_handle);101 rc = gpt_write_partitions(data->gpt.parts, data->gpt.gpt, dev_handle); 112 102 if (rc != EOK) { 113 103 printf("Error: Writing partitions failed: %d (%s)\n", rc, str_error(rc)); … … 115 105 } 116 106 117 rc = gpt_write_gpt_header(data->gpt ->gpt, dev_handle);107 rc = gpt_write_gpt_header(data->gpt.gpt, dev_handle); 118 108 if (rc != EOK) { 119 109 printf("Error: Writing partitions failed: %d (%s)\n", rc, str_error(rc)); … … 124 114 } 125 115 126 int extra_gpt_funcs(tinput_t * in, service_id_t dev_handle, union label_data * data)116 int extra_gpt_funcs(tinput_t * in, service_id_t dev_handle, union table_data * data) 127 117 { 128 118 printf("Not implemented.\n"); -
uspace/app/hdisk/func_gpt.h
rc9f61150 r9bda5d90 42 42 #include "common.h" 43 43 44 extern int add_gpt_part(tinput_t *, union label_data *); 45 extern int delete_gpt_part(tinput_t *, union label_data *); 46 extern int destroy_gpt_label(union label_data *); 47 extern int new_gpt_label(union label_data *); 48 extern int print_gpt_parts(union label_data *); 49 extern int read_gpt_parts(service_id_t, union label_data *); 50 extern int write_gpt_parts(service_id_t, union label_data *); 51 extern int extra_gpt_funcs(tinput_t *, service_id_t, union label_data *); 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 new_gpt_table(tinput_t *, union table_data *); 47 extern int print_gpt_parts(union table_data * data); 48 extern int write_gpt_parts(service_id_t dev_handle, union table_data * data); 49 extern int extra_gpt_funcs(tinput_t * in, service_id_t dev_handle, union table_data * data); 52 50 53 51 #endif -
uspace/app/hdisk/func_mbr.c
rc9f61150 r9bda5d90 41 41 #include "input.h" 42 42 43 static int set_mbr_partition(tinput_t *in, mbr_part_t *p); 44 45 int add_mbr_part(tinput_t *in, union label_data *data) 43 static int set_mbr_partition(tinput_t * in, mbr_part_t * p); 44 45 46 int add_mbr_part(tinput_t * in, union table_data * data) 46 47 { 47 48 int rc; 48 49 49 mbr_part_t * part = mbr_alloc_partition();50 mbr_part_t * part = mbr_alloc_partition(); 50 51 51 52 set_mbr_partition(in, part); 52 53 53 rc = mbr_add_partition(data->mbr , part);54 rc = mbr_add_partition(data->mbr.parts, part); 54 55 if (rc != EOK) { 55 56 printf("Error adding partition.\n"); 56 57 } 57 58 58 return EOK; 59 } 60 61 int delete_mbr_part(tinput_t *in, union label_data *data) 59 60 return rc; 61 } 62 63 int delete_mbr_part(tinput_t * in, union table_data * data) 62 64 { 63 65 int rc; … … 70 72 return errno; 71 73 72 rc = mbr_remove_partition(data->mbr , idx);74 rc = mbr_remove_partition(data->mbr.parts, idx); 73 75 if(rc != EOK) { 74 76 printf("Error: something.\n"); … … 78 80 } 79 81 80 int destroy_mbr_label(union label_data *data) 81 { 82 mbr_free_label(data->mbr); 83 return EOK; 84 } 85 86 int new_mbr_label(union label_data *data) 87 { 88 data->mbr = mbr_alloc_label(); 89 if (data->mbr == NULL) 90 return ENOMEM; 91 else 92 return EOK; 82 int new_mbr_table(tinput_t * in, union table_data * data) 83 { 84 data->mbr.mbr = mbr_alloc_mbr(); 85 data->mbr.parts = mbr_alloc_partitions(); 86 return EOK; 93 87 } 94 88 95 89 /** Print current partition scheme */ 96 int print_mbr_parts(union label_data *data)90 int print_mbr_parts(union table_data * data) 97 91 { 98 92 int num = 0; … … 102 96 printf("\t\t%10s %10s %10s %10s %7s\n", "Bootable:", "Start:", "End:", "Length:", "Type:"); 103 97 104 mbr_part_t * it;105 mbr_part_foreach(data->mbr ->parts, it) {98 mbr_part_t * it; 99 mbr_part_foreach(data->mbr.parts, it) { 106 100 if (it->type == PT_UNUSED) 107 101 continue; … … 123 117 } 124 118 125 int read_mbr_parts(service_id_t dev_handle, union label_data *data) 126 { 127 int rc; 128 printf("mbr\n"); 129 rc = mbr_read_mbr(data->mbr, dev_handle); 130 if (rc != EOK) 131 return rc; 132 printf("ismbr\n"); 133 if (!mbr_is_mbr(data->mbr)) 134 return EINVAL; 135 printf("parts\n"); 136 rc = mbr_read_partitions(data->mbr); 137 if (rc != EOK) 138 return rc; 139 printf("end\n"); 140 return EOK; 141 } 142 143 int write_mbr_parts(service_id_t dev_handle, union label_data *data) 144 { 145 int rc = mbr_write_partitions(data->mbr, dev_handle); 119 int write_mbr_parts(service_id_t dev_handle, union table_data * data) 120 { 121 int rc = mbr_write_partitions(data->mbr.parts, data->mbr.mbr, dev_handle); 146 122 if (rc != EOK) { 147 123 printf("Error occured during writing: ERR: %d: %s\n", rc, str_error(rc)); … … 151 127 } 152 128 153 int extra_mbr_funcs(tinput_t * in, service_id_t dev_handle, union label_data *data)129 int extra_mbr_funcs(tinput_t * in, service_id_t dev_handle, union table_data * data) 154 130 { 155 131 printf("Not implemented.\n"); … … 157 133 } 158 134 159 static int set_mbr_partition(tinput_t * in, mbr_part_t *p)135 static int set_mbr_partition(tinput_t * in, mbr_part_t * p) 160 136 { 161 137 int c; … … 183 159 return errno; 184 160 185 ///TODO: there can only be one boo labelpartition; let's do it just like fdisk161 ///TODO: there can only be one bootable partition; let's do it just like fdisk 186 162 printf("Bootable? (y/n): "); 187 163 c = getchar(); -
uspace/app/hdisk/func_mbr.h
rc9f61150 r9bda5d90 42 42 #include "common.h" 43 43 44 extern int add_mbr_part(tinput_t *, union label_data *); 45 extern int delete_mbr_part(tinput_t *, union label_data *); 46 extern int destroy_mbr_label(union label_data *); 47 extern int new_mbr_label(union label_data *); 48 extern int print_mbr_parts(union label_data *); 49 extern int read_mbr_parts(service_id_t, union label_data *); 50 extern int write_mbr_parts(service_id_t, union label_data *); 51 extern int extra_mbr_funcs(tinput_t *, service_id_t, union label_data *); 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 new_mbr_table(tinput_t *, union table_data *); 47 extern int print_mbr_parts(union table_data * data); 48 extern int write_mbr_parts(service_id_t dev_handle, union table_data * data); 49 extern int extra_mbr_funcs(tinput_t * in, service_id_t dev_handle, union table_data * data); 52 50 53 51 #endif -
uspace/app/hdisk/func_none.c
rc9f61150 r9bda5d90 40 40 static void not_implemented(void); 41 41 42 43 int add_none_part(tinput_t *in, union label_data *data) 42 int add_none_part(tinput_t * in, union table_data * data) 44 43 { 45 44 not_implemented(); … … 47 46 } 48 47 49 int delete_none_part(tinput_t * in, union label_data *data)48 int delete_none_part(tinput_t * in, union table_data * data) 50 49 { 51 50 not_implemented(); … … 53 52 } 54 53 55 int destroy_none_label(union label_data *data)54 int new_none_table(tinput_t * in, union table_data * data) 56 55 { 57 56 not_implemented(); … … 59 58 } 60 59 61 int new_none_label(union label_data *data)60 int print_none_parts(union table_data * data) 62 61 { 63 62 not_implemented(); … … 65 64 } 66 65 67 int print_none_parts(union label_data *data)66 int write_none_parts(service_id_t dev_handle, union table_data * data) 68 67 { 69 68 not_implemented(); … … 71 70 } 72 71 73 int read_none_parts(service_id_t dev_handle, union label_data *data) 74 { 75 not_implemented(); 76 return EOK; 77 } 78 79 int write_none_parts(service_id_t dev_handle, union label_data *data) 80 { 81 not_implemented(); 82 return EOK; 83 } 84 85 int extra_none_funcs(tinput_t *in, service_id_t dev_handle, union label_data *data) 72 int extra_none_funcs(tinput_t * in, service_id_t dev_handle, union table_data * data) 86 73 { 87 74 not_implemented(); -
uspace/app/hdisk/func_none.h
rc9f61150 r9bda5d90 41 41 #include "common.h" 42 42 43 extern int add_none_part(tinput_t *, union label_data *); 44 extern int delete_none_part(tinput_t *, union label_data *); 45 extern int destroy_none_label(union label_data *); 46 extern int new_none_label(union label_data *); 47 extern int print_none_parts(union label_data *); 48 extern int read_none_parts(service_id_t, union label_data *); 49 extern int write_none_parts(service_id_t, union label_data *); 50 extern int extra_none_funcs(tinput_t *, service_id_t, union label_data *); 43 extern int add_none_part(tinput_t * in, union table_data * data); 44 extern int delete_none_part(tinput_t * in, union table_data * data); 45 extern int new_none_table(tinput_t *, union table_data *); 46 extern int print_none_parts(union table_data * data); 47 extern int write_none_parts(service_id_t dev_handle, union table_data * data); 48 extern int extra_none_funcs(tinput_t * in, service_id_t dev_handle, union table_data * data); 51 49 52 50 #endif -
uspace/app/hdisk/hdisk.c
rc9f61150 r9bda5d90 53 53 #include "func_none.h" 54 54 55 int interact(service_id_t );55 int interact(service_id_t dev_handle); 56 56 void print_help(void); 57 void select_label_format(tinput_t *); 58 void fill_label_funcs(void); 59 void free_label(void); 60 int try_read(service_id_t); 61 62 int construct_none_label(void); 63 64 int construct_mbr_label(void); 65 int try_read_mbr(service_id_t); 66 67 int construct_gpt_label(void); 68 int try_read_gpt(service_id_t); 69 70 71 static label_t label; 57 void select_table_format(tinput_t * in); 58 void fill_table_funcs(void); 59 void free_table(void); 60 61 static table_t table; 72 62 73 63 int main(int argc, char ** argv) 74 64 { 75 65 if (argc == 1) { 76 printf(" Missing argument. Please specify a device to operate on.\n");77 return -1;78 } 79 66 printf("I'd like to have an argument, please.\n"); 67 return 1; 68 } 69 80 70 int rc; 81 71 service_id_t dev_handle; 82 72 83 73 rc = loc_service_get_id(argv[1], &dev_handle, IPC_FLAG_BLOCKING); 84 74 if (rc != EOK) { … … 86 76 return -1; 87 77 } 88 89 printf("Init.\n"); 90 init_label(); 91 92 /* 78 79 init_table(); 80 93 81 mbr_t * mbr = mbr_read_mbr(dev_handle); 94 82 if(mbr == NULL) { … … 99 87 100 88 if(mbr_is_mbr(mbr)) { 101 label.layout = LYT_MBR;102 set_ label_mbr(mbr);89 table.layout = LYT_MBR; 90 set_table_mbr(mbr); 103 91 mbr_partitions_t * parts = mbr_read_partitions(mbr); 104 92 if(parts == NULL) { … … 107 95 parts = mbr_alloc_partitions(); 108 96 } 109 set_ label_mbr_parts(parts);110 fill_ label_funcs();97 set_table_mbr_parts(parts); 98 fill_table_funcs(); 111 99 goto interact; 112 100 } 113 101 114 102 115 mbr_free_mbr(mbr);*/ 116 117 printf("Try MBR.\n"); 118 rc = try_read_mbr(dev_handle); 119 if (rc == EOK) 120 goto interact; 121 122 /* 103 mbr_free_mbr(mbr); 123 104 gpt_t * gpt = gpt_read_gpt_header(dev_handle); 124 105 125 106 if(gpt != NULL) { 126 label.layout = LYT_GPT;127 set_ label_gpt(gpt);107 table.layout = LYT_GPT; 108 set_table_gpt(gpt); 128 109 129 110 gpt_partitions_t * parts = gpt_read_partitions(gpt); … … 134 115 parts = gpt_alloc_partitions(); 135 116 } 136 set_ label_gpt_parts(parts);137 fill_ label_funcs();117 set_table_gpt_parts(parts); 118 fill_table_funcs(); 138 119 goto interact; 139 120 } 140 */ 141 142 printf("Try GPT.\n"); 143 rc = try_read_gpt(dev_handle); 144 if (rc == EOK) 145 goto interact; 146 147 printf("No label recognized. Create a new one.\n"); 148 label.layout = LYT_NONE; 121 printf("No partition table recognized. Create a new one.\n"); 122 table.layout = LYT_NONE; 149 123 150 124 interact: 151 printf("interact.\n");152 125 rc = interact(dev_handle); 153 126 154 free_ label();127 free_table(); 155 128 156 129 return rc; … … 179 152 switch(input) { 180 153 case 'a': 181 label.add_part(in, &label.data); 182 break; 183 case 'b': 184 label.add_part(in, &label.data); 154 table.add_part(in, &table.data); 185 155 break; 186 156 case 'd': 187 label.delete_part(in, &label.data);157 table.delete_part(in, &table.data); 188 158 break; 189 159 case 'e': 190 label.extra_funcs(in, dev_handle, &label.data);160 table.extra_funcs(in, dev_handle, &table.data); 191 161 break; 192 162 case 'f': 193 free_ label();194 select_ label_format(in);163 free_table(); 164 select_table_format(in); 195 165 break; 196 166 case 'h': … … 198 168 break; 199 169 case 'n': 200 free_ label();201 label.new_label(&label.data);170 free_table(); 171 table.new_table(in, &table.data); 202 172 break; 203 173 case 'p': 204 label.print_parts(&label.data);174 table.print_parts(&table.data); 205 175 break; 206 176 case 'q': … … 208 178 goto end; 209 179 case 'w': 210 label.write_parts(dev_handle, &label.data);180 table.write_parts(dev_handle, &table.data); 211 181 break; 212 182 default: … … 227 197 "\t 'a' \t\t Add partition.\n" 228 198 "\t 'd' \t\t Delete partition.\n" 229 "\t 'e' \t\t Extra functions (per labelformat).\n"230 "\t 'f' \t\t Switch the format of the partition label."199 "\t 'e' \t\t Extra functions (per table format).\n" 200 "\t 'f' \t\t Switch the format of the partition table." 231 201 "\t 'h' \t\t Prints help. See help for more.\n" 232 "\t 'l' \t\t Set alignment.\n" 233 "\t 'n' \t\t Create new label (discarding the old one).\n" 234 "\t 'p' \t\t Prints label contents.\n" 235 "\t 'w' \t\t Write label to disk.\n" 202 "\t 'n' \t\t Create new partition table (discarding the old one).\n" 203 "\t 'p' \t\t Prints the table contents.\n" 204 "\t 'w' \t\t Write table to disk.\n" 236 205 "\t 'q' \t\t Quit.\n" 237 206 ); … … 239 208 } 240 209 241 void select_ label_format(tinput_t * in)210 void select_table_format(tinput_t * in) 242 211 { 243 212 printf("Available formats are: \n" … … 249 218 switch(val) { 250 219 case 0: 251 free_label(); 252 label.layout = LYT_NONE; 253 fill_label_funcs(); 220 table.layout = LYT_NONE; 221 fill_table_funcs(); 254 222 break; 255 223 case 1: 256 free_label(); 257 label.layout = LYT_MBR; 258 fill_label_funcs(); 224 table.layout = LYT_MBR; 225 fill_table_funcs(); 259 226 break; 260 227 case 2: 261 free_label(); 262 label.layout = LYT_GPT; 263 fill_label_funcs(); 264 break; 265 } 266 } 267 268 void fill_label_funcs(void) 269 { 270 switch(label.layout) { 228 table.layout = LYT_GPT; 229 fill_table_funcs(); 230 break; 231 } 232 } 233 234 void fill_table_funcs(void) 235 { 236 switch(table.layout) { 271 237 case LYT_MBR: 272 construct_mbr_label(); 238 table.add_part = add_mbr_part; 239 table.delete_part = delete_mbr_part; 240 table.new_table = new_mbr_table; 241 table.print_parts = print_mbr_parts; 242 table.write_parts = write_mbr_parts; 243 table.extra_funcs = extra_mbr_funcs; 273 244 break; 274 245 case LYT_GPT: 275 construct_gpt_label(); 246 table.add_part = add_gpt_part; 247 table.delete_part = delete_gpt_part; 248 table.new_table = new_gpt_table; 249 table.print_parts = print_gpt_parts; 250 table.write_parts = write_gpt_parts; 251 table.extra_funcs = extra_gpt_funcs; 276 252 break; 277 253 default: 278 construct_none_label(); 279 break; 280 } 281 } 282 283 void free_label(void) 284 { 285 /* 286 switch(label.layout) { 254 table.add_part = add_none_part; 255 table.delete_part = delete_none_part; 256 table.new_table = new_none_table; 257 table.print_parts = print_none_parts; 258 table.write_parts = write_none_parts; 259 table.extra_funcs = extra_none_funcs; 260 break; 261 } 262 } 263 264 void free_table(void) 265 { 266 switch(table.layout) { 287 267 case LYT_MBR: 288 destroy_mbr_label(&label); 268 if (table.data.mbr.parts != NULL) { 269 mbr_free_partitions(table.data.mbr.parts); 270 table.data.mbr.parts = NULL; 271 } 272 if (table.data.mbr.mbr != NULL) { 273 mbr_free_mbr(table.data.mbr.mbr); 274 table.data.mbr.mbr = NULL; 275 } 289 276 break; 290 277 case LYT_GPT: 291 destroy_gpt_label(&label); 278 if (table.data.gpt.parts != NULL) { 279 gpt_free_partitions(table.data.gpt.parts); 280 table.data.gpt.parts = NULL; 281 } 282 if (table.data.gpt.gpt != NULL) { 283 gpt_free_gpt(table.data.gpt.gpt); 284 table.data.gpt.gpt = NULL; 285 } 292 286 break; 293 287 default: 294 288 break; 295 289 } 296 */ 297 298 label.destroy_label(&label.data); 299 } 300 301 int try_read(service_id_t dev_handle) 302 { 303 fill_label_funcs(); 304 printf("read_parts\n"); 305 return label.read_parts(dev_handle, &label.data); 306 } 307 308 int construct_none_label() 309 { 310 label.add_part = add_none_part; 311 label.delete_part = delete_none_part; 312 label.destroy_label = destroy_none_label; 313 label.new_label = new_none_label; 314 label.print_parts = print_none_parts; 315 label.read_parts = read_none_parts; 316 label.write_parts = write_none_parts; 317 label.extra_funcs = extra_none_funcs; 318 319 return EOK; 320 } 321 322 int construct_mbr_label() 323 { 324 label.add_part = add_mbr_part; 325 label.delete_part = delete_mbr_part; 326 label.destroy_label = destroy_mbr_label; 327 label.new_label = new_mbr_label; 328 label.print_parts = print_mbr_parts; 329 label.read_parts = read_mbr_parts; 330 label.write_parts = write_mbr_parts; 331 label.extra_funcs = extra_mbr_funcs; 332 333 return label.new_label(&label.data); 334 } 335 336 int try_read_mbr(service_id_t dev_handle) 337 { 338 label.layout = LYT_MBR; 339 return try_read(dev_handle); 340 } 341 342 int construct_gpt_label() 343 { 344 label.add_part = add_gpt_part; 345 label.delete_part = delete_gpt_part; 346 label.new_label = new_gpt_label; 347 label.print_parts = print_gpt_parts; 348 label.read_parts = read_gpt_parts; 349 label.write_parts = write_gpt_parts; 350 label.extra_funcs = extra_gpt_funcs; 351 352 return label.new_label(&label.data); 353 } 354 355 int try_read_gpt(service_id_t dev_handle) 356 { 357 label.layout = LYT_GPT; 358 return try_read(dev_handle); 359 } 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 290 } 291 292 293 -
uspace/app/hdisk/hdisk.h
rc9f61150 r9bda5d90 39 39 LYT_MBR, 40 40 LYT_GPT, 41 } layouts_t;41 } LAYOUTS; 42 42 43 typedef struct label { 44 layouts_t layout; 45 union label_data data; 46 int (* add_part) (tinput_t *, union label_data *); 47 int (* delete_part) (tinput_t *, union label_data *); 48 int (* destroy_label)( union label_data *); 49 int (* new_label) ( union label_data *); 50 int (* print_parts) ( union label_data *); 51 int (* read_parts) (service_id_t, union label_data *); 52 int (* write_parts) (service_id_t, union label_data *); 53 int (* extra_funcs) (tinput_t *, service_id_t, union label_data *); 54 } label_t; 43 typedef struct table { 44 LAYOUTS layout; 45 union table_data data; 46 int (* add_part)(tinput_t *, union table_data *); 47 int (* delete_part)(tinput_t *, union table_data *); 48 int (* new_table)(tinput_t *, union table_data *); 49 int (* print_parts)(); 50 int (* write_parts)(service_id_t, union table_data *); 51 int (* extra_funcs)(tinput_t *, service_id_t, union table_data *); 52 } table_t; 55 53 56 #define init_ label() \57 label.layout = LYT_NONE54 #define init_table() \ 55 table.layout = LYT_NONE 58 56 57 #define set_table_mbr(m) \ 58 table.data.mbr.mbr = (m) 59 60 #define set_table_mbr_parts(p) \ 61 table.data.mbr.parts = (p) 62 63 #define set_table_gpt(g) \ 64 table.data.gpt.gpt = (g) 65 66 #define set_table_gpt_parts(p) \ 67 table.data.gpt.parts = (p) -
uspace/lib/gpt/libgpt.h
rc9f61150 r9bda5d90 1 1 /* 2 * Copyright (c) 2009 Jiri Svoboda 3 * Copyright (c) 2011, 2012, 2013 Dominik Taborsky 2 * Copyright (c) 2009 Jiri Svoboda, 2011, 2012, 2013 Dominik Taborsky 4 3 * All rights reserved. 5 4 * … … 130 129 gpt_t * gpt; 131 130 gpt_partitions_t * parts; 132 } gpt_ label_t;131 } gpt_table_t; 133 132 134 133 struct partition_type { … … 142 141 extern gpt_t * gpt_alloc_gpt_header(void); 143 142 extern gpt_t * gpt_read_gpt_header(service_id_t dev_handle); 144 extern int 143 extern int gpt_write_gpt_header(gpt_t * header, service_id_t dev_handle); 145 144 146 extern gpt_partitions_t * 147 extern gpt_partitions_t * gpt_read_partitions(gpt_t * gpt);148 extern int gpt_write_partitions(gpt_partitions_t * parts, gpt_t * header, service_id_t dev_handle);149 extern gpt_part_t * gpt_alloc_partition(gpt_partitions_t * parts);150 extern int gpt_add_partition(gpt_partitions_t * parts, gpt_part_t * partition);151 extern int 145 extern gpt_partitions_t * gpt_alloc_partitions(void); 146 extern gpt_partitions_t * gpt_read_partitions (gpt_t * gpt); 147 extern int gpt_write_partitions (gpt_partitions_t * parts, gpt_t * header, service_id_t dev_handle); 148 extern gpt_part_t * gpt_alloc_partition (gpt_partitions_t * parts); 149 extern int gpt_add_partition (gpt_partitions_t * parts, gpt_part_t * partition); 150 extern int gpt_remove_partition(gpt_partitions_t * parts, size_t idx); 152 151 153 extern size_t gpt_get_part_type(gpt_part_t * p);154 extern void gpt_set_part_type(gpt_part_t * p, size_t type);155 extern void gpt_set_start_lba(gpt_part_t * p, uint64_t start);156 extern uint64_t gpt_get_start_lba(gpt_part_t * p);157 extern void gpt_set_end_lba(gpt_part_t * p, uint64_t end);158 extern uint64_t gpt_get_end_lba(gpt_part_t * p);159 extern unsigned char * gpt_get_part_name(gpt_part_t * p);160 extern void gpt_set_part_name(gpt_part_t * p, char * name[], size_t length);161 extern bool gpt_get_flag(gpt_part_t * p, GPT_ATTR flag);162 extern void gpt_set_flag(gpt_part_t * p, GPT_ATTR flag, bool value);152 extern size_t gpt_get_part_type (gpt_part_t * p); 153 extern void gpt_set_part_type (gpt_part_t * p, size_t type); 154 extern void gpt_set_start_lba (gpt_part_t * p, uint64_t start); 155 extern uint64_t gpt_get_start_lba (gpt_part_t * p); 156 extern void gpt_set_end_lba (gpt_part_t * p, uint64_t end); 157 extern uint64_t gpt_get_end_lba (gpt_part_t * p); 158 extern unsigned char * gpt_get_part_name (gpt_part_t * p); 159 extern void gpt_set_part_name (gpt_part_t * p, char * name[], size_t length); 160 extern bool gpt_get_flag (gpt_part_t * p, GPT_ATTR flag); 161 extern void gpt_set_flag (gpt_part_t * p, GPT_ATTR flag, bool value); 163 162 164 163 -
uspace/lib/mbr/libmbr.c
rc9f61150 r9bda5d90 46 46 47 47 static br_block_t * alloc_br(void); 48 static int decode_part(pt_entry_t *, mbr_part_t *, uint32_t); 49 static int decode_logical(mbr_label_t *, mbr_part_t *); 50 static void encode_part(mbr_part_t *, pt_entry_t *, uint32_t, bool); 51 static int check_overlap(mbr_part_t *, mbr_part_t *); 52 static int check_encaps(mbr_part_t *, mbr_part_t *); 53 static int check_preceeds(mbr_part_t *, mbr_part_t *); 54 55 /** Allocate and initialize mbr_label_t structure */ 56 mbr_label_t * mbr_alloc_label(void) 57 { 58 mbr_label_t *label = malloc(sizeof(mbr_label_t)); 59 if (label == NULL) 60 return NULL; 61 62 label->mbr = NULL; 63 label->parts = NULL; 64 label->device = 0; 65 66 return label; 67 } 68 69 /** Free mbr_label_t structure */ 70 void mbr_free_label(mbr_label_t *label) 71 { 72 if (label->mbr != NULL) 73 mbr_free_mbr(label->mbr); 74 75 if (label->parts != NULL) 76 mbr_free_partitions(label->parts); 77 78 free(label); 79 } 48 static int decode_part(pt_entry_t * src, mbr_part_t * trgt, uint32_t base); 49 static int decode_logical(mbr_t * mbr, mbr_partitions_t * p, mbr_part_t * ext); 50 static void encode_part(mbr_part_t * src, pt_entry_t * trgt, uint32_t base, bool ebr); 51 static int check_overlap(mbr_part_t * p1, mbr_part_t * p2); 52 static int check_encaps(mbr_part_t * inner, mbr_part_t * outer); 53 static int check_preceeds(mbr_part_t * preceeder, mbr_part_t * precedee); 80 54 81 55 /** Allocate memory for mbr_t */ … … 86 60 87 61 /** Read MBR from specific device 88 * @param label label to write data to 89 * @param dev_handle device to read MBR from 90 * 91 * @return EOK on success, error code on error 92 */ 93 int mbr_read_mbr(mbr_label_t *label, service_id_t dev_handle) 94 { 95 if (label == NULL) 96 return EINVAL; 97 62 * @param dev_handle device to read MBR from 63 * 64 * @return mbr record on success, NULL on error 65 */ 66 mbr_t * mbr_read_mbr(service_id_t dev_handle) 67 { 98 68 int rc; 99 100 if (label->mbr == NULL) { 101 label->mbr = mbr_alloc_mbr(); 102 if (label->mbr == NULL) { 103 return ENOMEM; 104 } 69 70 mbr_t * mbr = malloc(sizeof(mbr_t)); 71 if (mbr == NULL) { 72 return NULL; 105 73 } 106 74 107 75 rc = block_init(EXCHANGE_ATOMIC, dev_handle, 512); 108 if (rc != EOK) 109 return rc; 110 111 rc = block_read_direct(dev_handle, 0, 1, &(label->mbr->raw_data)); 76 if (rc != EOK) { 77 free(mbr); 78 return NULL; 79 } 80 81 rc = block_read_direct(dev_handle, 0, 1, &(mbr->raw_data)); 82 if (rc != EOK) { 83 free(mbr); 84 block_fini(dev_handle); 85 return NULL; 86 } 87 112 88 block_fini(dev_handle); 113 if (rc != EOK) 114 return rc; 115 116 label->device = dev_handle; 117 118 return EOK; 89 90 mbr->device = dev_handle; 91 92 return mbr; 119 93 } 120 94 121 95 /** Write mbr to disk 122 * @param labelMBR to be written96 * @param mbr MBR to be written 123 97 * @param dev_handle device handle to write MBR to (may be different 124 98 * from the device in 'mbr') … … 126 100 * @return 0 on success, otherwise libblock error code 127 101 */ 128 int mbr_write_mbr(mbr_ label_t *label, service_id_t dev_handle)102 int mbr_write_mbr(mbr_t * mbr, service_id_t dev_handle) 129 103 { 130 104 int rc; … … 135 109 } 136 110 137 rc = block_write_direct(dev_handle, 0, 1, &( label->mbr->raw_data));111 rc = block_write_direct(dev_handle, 0, 1, &(mbr->raw_data)); 138 112 block_fini(dev_handle); 139 113 if (rc != EOK) { … … 141 115 } 142 116 143 return EOK;117 return 0; 144 118 } 145 119 … … 150 124 * @return 1 if MBR, 0 if GPT 151 125 */ 152 int mbr_is_mbr(mbr_label_t *label) 153 { 154 return (label->mbr->raw_data.pte[0].ptype != PT_GPT) ? 1 : 0; 155 } 156 157 /** Parse partitions from MBR, freeing previous partitions if any 158 * NOTE: it is assumed mbr_read_mbr(label) was called before. 159 * @param label MBR to be parsed 160 * 161 * @return linked list of partitions or NULL on error 162 */ 163 int mbr_read_partitions(mbr_label_t *label) 164 { 165 if (label == NULL || label->mbr == NULL) 166 return EINVAL; 167 168 int rc, rc_ext; 169 unsigned int i; 170 mbr_part_t *p; 171 mbr_part_t *ext = NULL; 172 //mbr_partitions_t *parts; 173 printf("check\n"); 174 if (label->parts != NULL) 175 mbr_free_partitions(label->parts); 176 printf("check2\n"); 177 label->parts = mbr_alloc_partitions(); 178 if (label->parts == NULL) { 179 return ENOMEM; 180 } 181 printf("primary\n"); 182 /* Generate the primary partitions */ 126 int mbr_is_mbr(mbr_t * mbr) 127 { 128 return (mbr->raw_data.pte[0].ptype != PT_GPT) ? 1 : 0; 129 } 130 131 /** Parse partitions from MBR 132 * @param mbr MBR to be parsed 133 * 134 * @return linked list of partitions or NULL on error 135 */ 136 mbr_partitions_t * mbr_read_partitions(mbr_t * mbr) 137 { 138 int rc, i, rc_ext; 139 mbr_part_t * p; 140 mbr_part_t * ext = NULL; 141 mbr_partitions_t * parts; 142 143 if (mbr == NULL) 144 return NULL; 145 146 parts = mbr_alloc_partitions(); 147 if (parts == NULL) { 148 return NULL; 149 } 150 151 // Generate the primary partitions 183 152 for (i = 0; i < N_PRIMARY; ++i) { 184 if ( label->mbr->raw_data.pte[i].ptype == PT_UNUSED)153 if (mbr->raw_data.pte[i].ptype == PT_UNUSED) 185 154 continue; 186 printf("pcheck1\n");155 187 156 p = mbr_alloc_partition(); 188 157 if (p == NULL) { 189 158 printf(LIBMBR_NAME ": Error on memory allocation.\n"); 190 mbr_free_partitions( label->parts);191 return ENOMEM;192 } 193 printf("pcheck2\n");194 rc_ext = decode_part(&( label->mbr->raw_data.pte[i]), p, 0);159 mbr_free_partitions(parts); 160 return NULL; 161 } 162 163 rc_ext = decode_part(&(mbr->raw_data.pte[i]), p, 0); 195 164 mbr_set_flag(p, ST_LOGIC, false); 196 rc = mbr_add_partition( label, p);165 rc = mbr_add_partition(parts, p); 197 166 if (rc != ERR_OK) { 198 167 printf(LIBMBR_NAME ": Error occured during decoding the MBR. (%d)\n" \ 199 LIBMBR_NAME ": MBR is invalid.\n", rc); 200 mbr_free_partitions(label->parts); 201 return EINVAL; 202 } 203 printf("pcheck3\n"); 168 LIBMBR_NAME ": Partition list may be incomplete.\n", rc); 169 return NULL; 170 } 171 204 172 if (rc_ext) { 205 173 ext = p; 206 label->parts->l_extended = list_nth(&(label->parts->list), i); 207 } 208 printf("pcheck4\n"); 209 } 210 printf("logical\n"); 211 /* Fill in the primary partitions and generate logical ones, if any */ 212 rc = decode_logical(label, ext); 174 parts->l_extended = list_last(&(parts->list)); 175 } 176 } 177 178 // Fill in the primary partitions and generate logical ones, if any 179 rc = decode_logical(mbr, parts, ext); 213 180 if (rc != EOK) { 214 181 printf(LIBMBR_NAME ": Error occured during decoding the MBR.\n" \ 215 182 LIBMBR_NAME ": Partition list may be incomplete.\n"); 216 return rc; 217 } 218 printf("finish\n"); 219 return EOK; 183 } 184 185 return parts; 220 186 } 221 187 222 188 /** Write MBR and partitions to device 223 * @param label label to write 224 * @param dev_handle device to write the data to 225 * 226 * @return returns EOK on succes, specific error code otherwise 227 */ 228 int mbr_write_partitions(mbr_label_t *label, service_id_t dev_handle) 189 * @param parts partition list to be written 190 * @param mbr MBR to be written with 'parts' partitions 191 * @param dev_handle device to write the data to 192 * 193 * @return returns EOK on succes, specific error code otherwise 194 */ 195 int mbr_write_partitions(mbr_partitions_t * parts, mbr_t * mbr, service_id_t dev_handle) 229 196 { 230 197 int i = 0; 231 198 int rc; 232 mbr_part_t * p;233 mbr_part_t * ext = (label->parts->l_extended == NULL) ? NULL234 : list_get_instance( label->parts->l_extended, mbr_part_t, link);199 mbr_part_t * p; 200 mbr_part_t * ext = (parts->l_extended == NULL) ? NULL 201 : list_get_instance(parts->l_extended, mbr_part_t, link); 235 202 236 203 rc = block_init(EXCHANGE_ATOMIC, dev_handle, 512); … … 240 207 } 241 208 242 link_t * l = label->parts->list.head.next;243 244 / * Encoding primary partitions */245 for (i = 0; i < label->parts->n_primary; i++) {246 p = list_get_instance(l, mbr_part_t, link); 247 encode_part(p, &( label->mbr->raw_data.pte[i]), 0, false);209 link_t * l = parts->list.head.next; 210 211 // Encoding primary partitions 212 for (i = 0; i < parts->n_primary; i++) { 213 p = list_get_instance(l, mbr_part_t, link); 214 encode_part(p, &(mbr->raw_data.pte[i]), 0, false); 248 215 l = l->next; 249 216 } 250 217 251 / * Writing MBR */252 rc = block_write_direct(dev_handle, 0, 1, &( label->mbr->raw_data));218 // Writing MBR 219 rc = block_write_direct(dev_handle, 0, 1, &(mbr->raw_data)); 253 220 if (rc != EOK) { 254 221 printf(LIBMBR_NAME ": Error while writing MBR : %d - %s.\n", rc, str_error(rc)); … … 267 234 * as much power over it as you can get. Thanks. */ 268 235 269 / * Encoding and writing first logical partition */270 if (l != &( label->parts->list.head)) {236 // Encoding and writing first logical partition 237 if (l != &(parts->list.head)) { 271 238 p = list_get_instance(l, mbr_part_t, link); 272 239 p->ebr_addr = base; … … 289 256 prev_p = p; 290 257 291 / * Encoding and writing logical partitions */292 while (l != &( label->parts->list.head)) {258 // Encoding and writing logical partitions 259 while (l != &(parts->list.head)) { 293 260 p = list_get_instance(l, mbr_part_t, link); 294 261 … … 319 286 } 320 287 321 / * write the last EBR */288 // write the last EBR 322 289 encode_part(NULL, &(prev_p->ebr->pte[1]), 0, false); 323 290 rc = block_write_direct(dev_handle, prev_p->ebr_addr, 1, prev_p->ebr); … … 338 305 mbr_part_t * mbr_alloc_partition(void) 339 306 { 340 mbr_part_t * p = malloc(sizeof(mbr_part_t));307 mbr_part_t * p = malloc(sizeof(mbr_part_t)); 341 308 if (p == NULL) { 342 309 return NULL; … … 345 312 link_initialize(&(p->link)); 346 313 p->ebr = NULL; 347 p->type = PT_UNUSED;314 p->type = 0; 348 315 p->status = 0; 349 316 p->start_addr = 0; 350 317 p->length = 0; 351 318 p->ebr_addr = 0; 352 319 353 320 return p; 354 321 } … … 357 324 mbr_partitions_t * mbr_alloc_partitions(void) 358 325 { 359 mbr_partitions_t * parts = malloc(sizeof(mbr_partitions_t));326 mbr_partitions_t * parts = malloc(sizeof(mbr_partitions_t)); 360 327 if (parts == NULL) { 361 328 return NULL; 362 329 } 363 330 364 331 list_initialize(&(parts->list)); 365 332 parts->n_primary = 0; 366 333 parts->n_logical = 0; 367 334 parts->l_extended = NULL; 368 369 /* add blank primary partitions */370 int i;371 mbr_part_t *p;372 for (i = 0; i < N_PRIMARY; ++i) {373 p = mbr_alloc_partition();374 if (p == NULL) {375 mbr_free_partitions(parts);376 return NULL;377 }378 list_append(&(p->link), &(parts->list));379 }380 381 335 382 336 return parts; … … 386 340 * Performs checks, sorts the list. 387 341 * 388 * @param label labelto add to342 * @param parts partition list to add to 389 343 * @param p partition to add 390 344 * 391 345 * @return ERR_OK (0) on success, other MBR_ERR_VAL otherwise 392 346 */ 393 mbr_err_val mbr_add_partition(mbr_label_t *label, mbr_part_t *p) 394 { 395 int rc; 396 mbr_partitions_t *parts = label->parts; 397 398 aoff64_t nblocks; 399 printf("add1.\n"); 400 rc = block_init(EXCHANGE_ATOMIC, label->device, 512); 401 if (rc != EOK) { 402 printf(LIBMBR_NAME ": Error while getting number of blocks: %d - %s.\n", rc, str_error(rc)); 403 return ERR_LIBBLOCK; 404 } 405 printf("add2.\n"); 406 rc = block_get_nblocks(label->device, &nblocks); 407 block_fini(label->device); 408 if (rc != EOK) { 409 printf(LIBMBR_NAME ": Error while getting number of blocks: %d - %s.\n", rc, str_error(rc)); 410 return ERR_LIBBLOCK; 411 } 412 printf("add3.\n"); 413 if (mbr_get_flag(p, ST_LOGIC)) { 414 /* adding logical partition */ 415 416 /* is there any extended partition? */ 347 MBR_ERR_VAL mbr_add_partition(mbr_partitions_t * parts, mbr_part_t * p) 348 { 349 if (mbr_get_flag(p, ST_LOGIC)) { // adding logical part 350 // is there any extended partition? 417 351 if (parts->l_extended == NULL) 418 352 return ERR_NO_EXTENDED; 419 353 420 / * is the logical partition inside the extended one? */421 mbr_part_t * ext = list_get_instance(parts->l_extended, mbr_part_t, link);354 // is the logical partition inside the extended one? 355 mbr_part_t * ext = list_get_instance(parts->l_extended, mbr_part_t, link); 422 356 if (!check_encaps(p, ext)) 423 357 return ERR_OUT_BOUNDS; 424 358 425 / * find a place for the new partition in a sorted linked list */426 //mbr_part_t *last = list_get_instance(list_last(&(parts->list)), mbr_part_t, link);427 mbr_part_t * iter;428 //uint32_t ebr_space = 1;359 // find a place for the new partition in a sorted linked list 360 mbr_part_t * last = list_get_instance(list_last(&(parts->list)), mbr_part_t, link); 361 mbr_part_t * iter; 362 uint32_t ebr_space = 1; 429 363 mbr_part_foreach(parts, iter) { 430 364 if (mbr_get_flag(iter, ST_LOGIC)) { … … 432 366 return ERR_OVERLAP; 433 367 if (check_preceeds(iter, p)) { 434 /* checking if there's at least one sector of space preceeding */ 435 if ((iter->start_addr + iter->length) >= p->start_addr - 1) 436 return ERR_NO_EBR; 437 } else { 438 /* checking if there's at least one sector of space following (for following partitions's EBR) */ 439 if ((p->start_addr + p->length) >= iter->start_addr - 1) 440 return ERR_NO_EBR; 441 } 368 last = iter; 369 ebr_space = p->start_addr - (last->start_addr + last->length); 370 } else 371 break; 442 372 } 443 373 } 444 374 445 /* alloc EBR if it's not already there */ 375 // checking if there's at least one sector of space preceeding 376 if (ebr_space < 1) 377 return ERR_NO_EBR; 378 379 // checking if there's at least one sector of space following (for following partitions's EBR) 380 if (last->link.next != &(parts->list.head)) { 381 if (list_get_instance(&(last->link.next), mbr_part_t, link)->start_addr <= p->start_addr + p->length + 1) 382 return ERR_NO_EBR; 383 } 384 385 // alloc EBR if it's not already there 446 386 if (p->ebr == NULL) { 447 387 p->ebr = alloc_br(); … … 451 391 } 452 392 453 / * add it */454 list_ append(&(p->link), &(parts->list));393 // add it 394 list_insert_after(&(p->link), &(last->link)); 455 395 parts->n_logical += 1; 456 } else { 457 /* adding primary */ 458 396 } else { // adding primary 459 397 if (parts->n_primary == 4) { 460 398 return ERR_PRIMARY_FULL; 461 399 } 462 400 463 / * Check if partition makes space for MBR itself. */464 if (p->start_addr == 0 || ((aoff64_t) p->start_addr) + p->length >= nblocks) {401 // TODO: should we check if it's inside the drive's upper boundary? 402 if (p->start_addr == 0) { 465 403 return ERR_OUT_BOUNDS; 466 404 } 467 printf("add4.\n");468 / * if it's extended, is there any other one? */405 406 // if it's extended, is there any other one? 469 407 if ((p->type == PT_EXTENDED || p->type == PT_EXTENDED_LBA) && parts->l_extended != NULL) { 470 408 return ERR_EXTENDED_PRESENT; 471 409 } 472 printf("add5.\n"); 473 /* find a place and add it */ 474 mbr_part_t *iter; 475 mbr_part_t *empty = NULL; 476 mbr_part_foreach(parts, iter) { 477 printf("type: %x\n", iter->type); 478 if (iter->type == PT_UNUSED) { 479 if (empty == NULL) 480 empty = iter; 481 } else if (check_overlap(p, iter)) 482 return ERR_OVERLAP; 483 } 484 printf("add6. %p, %p\n", empty, p); 485 list_insert_after(&(p->link), &(empty->link)); 486 printf("add6.1.\n"); 487 list_remove(&(empty->link)); 488 printf("add6.2.\n"); 489 free(empty); 490 printf("add7.\n"); 410 411 // find a place and add it 412 if (list_empty(&(parts->list))) { 413 list_append(&(p->link), &(parts->list)); 414 } else { 415 mbr_part_t * iter; 416 mbr_part_foreach(parts, iter) { 417 if (mbr_get_flag(iter, ST_LOGIC)) { 418 list_insert_before(&(p->link), &(iter->link)); 419 break; 420 } else if (check_overlap(p, iter)) 421 return ERR_OVERLAP; 422 } 423 if (iter == list_get_instance(&(parts->list.head.prev), mbr_part_t, link)) 424 list_append(&(p->link), &(parts->list)); 425 } 491 426 parts->n_primary += 1; 492 427 … … 494 429 parts->l_extended = &(p->link); 495 430 } 496 printf("add8.\n"); 431 497 432 return ERR_OK; 498 433 } … … 500 435 /** Remove partition 501 436 * Removes partition by index, indexed from zero. When removing extended 502 * 437 * partition, all logical partitions get removed as well. 503 438 * 504 * @param label labelto remove from439 * @param parts partition list to remove from 505 440 * @param idx index of the partition to remove 506 441 * 507 442 * @return EOK on success, EINVAL if idx invalid 508 443 */ 509 int mbr_remove_partition(mbr_ label_t *label, size_t idx)510 { 511 link_t * l = list_nth(&(label->parts->list), idx);444 int mbr_remove_partition(mbr_partitions_t * parts, size_t idx) 445 { 446 link_t * l = list_nth(&(parts->list), idx); 512 447 if (l == NULL) 513 448 return EINVAL; 514 449 515 mbr_part_t *p; 516 517 /* If we're removing an extended partition, remove all logical as well */ 518 if (l == label->parts->l_extended) { 519 label->parts->l_extended = NULL; 520 521 link_t *it = l->next; 522 link_t *next_it; 523 while (it != &(label->parts->list.head)) { 450 mbr_part_t * p; 451 452 /* TODO: if it is extended partition, should we also remove all logical? 453 * If we don't, we break the consistency of the list. If we do, 454 * the user will have to input them all over again. So yes. */ 455 if (l == parts->l_extended) { 456 parts->l_extended = NULL; 457 458 link_t * it = l->next; 459 link_t * next_it; 460 while (it != &(parts->list.head)) { 524 461 next_it = it->next; 525 462 … … 527 464 if (mbr_get_flag(p, ST_LOGIC)) { 528 465 list_remove(it); 529 label->parts->n_logical -= 1;466 parts->n_logical -= 1; 530 467 mbr_free_partition(p); 531 468 } … … 536 473 } 537 474 538 /* Remove the partition itself */ 475 list_remove(l); 476 539 477 p = list_get_instance(l, mbr_part_t, link); 540 if (mbr_get_flag(p, ST_LOGIC)) { 541 label->parts->n_logical -= 1; 542 list_remove(l); 543 mbr_free_partition(p); 544 } else { 545 /* Cannot remove primary - it would break ordering, just zero it */ 546 label->parts->n_primary -= 1; 547 p->type = 0; 548 p->status = 0; 549 p->start_addr = 0; 550 p->length = 0; 551 p->ebr_addr = 0; 552 } 478 if (mbr_get_flag(p, ST_LOGIC)) 479 parts->n_logical -= 1; 480 else 481 parts->n_primary -= 1; 482 483 484 mbr_free_partition(p); 553 485 554 486 return EOK; … … 556 488 557 489 /** mbr_part_t destructor */ 558 void mbr_free_partition(mbr_part_t * p)490 void mbr_free_partition(mbr_part_t * p) 559 491 { 560 492 if (p->ebr != NULL) … … 564 496 565 497 /** Get flag bool value */ 566 int mbr_get_flag(mbr_part_t * p, MBR_FLAGS flag)498 int mbr_get_flag(mbr_part_t * p, MBR_FLAGS flag) 567 499 { 568 500 return (p->status & (1 << flag)) ? 1 : 0; … … 570 502 571 503 /** Set a specifig status flag to a value */ 572 void mbr_set_flag(mbr_part_t * p, MBR_FLAGS flag, bool value)504 void mbr_set_flag(mbr_part_t * p, MBR_FLAGS flag, bool value) 573 505 { 574 506 uint8_t status = p->status; … … 590 522 591 523 /** Just a wrapper for free() */ 592 void mbr_free_mbr(mbr_t * mbr)524 void mbr_free_mbr(mbr_t * mbr) 593 525 { 594 526 free(mbr); … … 599 531 * @param parts partition list to be freed 600 532 */ 601 void mbr_free_partitions(mbr_partitions_t * parts)533 void mbr_free_partitions(mbr_partitions_t * parts) 602 534 { 603 535 list_foreach_safe(parts->list, cur_link, next) { 604 mbr_part_t *p = list_get_instance(cur_link, mbr_part_t, link); 536 mbr_part_t * p = list_get_instance(cur_link, mbr_part_t, link); 537 list_remove(cur_link); 605 538 mbr_free_partition(p); 606 539 } … … 611 544 // Internal functions follow // 612 545 613 static br_block_t * alloc_br()614 { 615 br_block_t * br = malloc(sizeof(br_block_t));546 static br_block_t * alloc_br() 547 { 548 br_block_t * br = malloc(sizeof(br_block_t)); 616 549 if (br == NULL) 617 550 return NULL; … … 626 559 * @return returns 1, if extended partition, 0 otherwise 627 560 * */ 628 static int decode_part(pt_entry_t * src, mbr_part_t *trgt, uint32_t base)561 static int decode_part(pt_entry_t * src, mbr_part_t * trgt, uint32_t base) 629 562 { 630 563 trgt->type = src->ptype; 631 564 632 565 /* Checking only 0x80; otherwise writing will fix to 0x00 */ 633 trgt->status = (trgt->status & 0xFF00) | src->status; 566 //trgt->bootable = (src->status == B_ACTIVE) ? true : false; 567 mbr_set_flag(trgt, ST_BOOT, (src->status == B_ACTIVE) ? true : false); 634 568 635 569 trgt->start_addr = uint32_t_le2host(src->first_lba) + base; … … 640 574 641 575 /** Parse MBR contents to mbr_part_t list */ 642 static int decode_logical(mbr_ label_t *label, mbr_part_t * ext)576 static int decode_logical(mbr_t * mbr, mbr_partitions_t * parts, mbr_part_t * ext) 643 577 { 644 578 int rc; 645 mbr_part_t *p; 579 mbr_part_t * p; 580 581 if (mbr == NULL || parts == NULL) 582 return EINVAL; 583 646 584 647 585 if (ext == NULL) 648 586 return EOK; 649 587 588 650 589 uint32_t base = ext->start_addr; 651 590 uint32_t addr = base; 652 br_block_t * ebr;653 654 rc = block_init(EXCHANGE_ATOMIC, label->device, 512);591 br_block_t * ebr; 592 593 rc = block_init(EXCHANGE_ATOMIC, mbr->device, 512); 655 594 if (rc != EOK) 656 595 return rc; … … 662 601 } 663 602 664 rc = block_read_direct( label->device, addr, 1, ebr);603 rc = block_read_direct(mbr->device, addr, 1, ebr); 665 604 if (rc != EOK) { 666 605 goto free_ebr_end; … … 687 626 p->ebr = ebr; 688 627 p->ebr_addr = addr; 689 rc = mbr_add_partition( label, p);628 rc = mbr_add_partition(parts, p); 690 629 if (rc != ERR_OK) 691 630 return EINVAL; … … 700 639 } 701 640 702 rc = block_read_direct( label->device, addr, 1, ebr);641 rc = block_read_direct(mbr->device, addr, 1, ebr); 703 642 if (rc != EOK) { 704 643 goto free_ebr_end; … … 721 660 p->ebr = ebr; 722 661 p->ebr_addr = addr; 723 rc = mbr_add_partition( label, p);662 rc = mbr_add_partition(parts, p); 724 663 if (rc != ERR_OK) 725 664 return EINVAL; … … 735 674 736 675 end: 737 block_fini( label->device);676 block_fini(mbr->device); 738 677 739 678 return rc; … … 744 683 { 745 684 if (src != NULL) { 746 //trgt->status = mbr_get_flag(src, ST_BOOT) ? B_ACTIVE : B_INACTIVE; 747 trgt->status = (uint8_t) (src->status & 0xFF); 748 /* ingoring CHS */ 749 trgt->first_chs[0] = 0xFE; 750 trgt->first_chs[1] = 0xFF; 751 trgt->first_chs[2] = 0xFF; 752 trgt->last_chs[0] = 0xFE; 753 trgt->last_chs[1] = 0xFF; 754 trgt->last_chs[2] = 0xFF; 685 trgt->status = mbr_get_flag(src, ST_BOOT) ? B_ACTIVE : B_INACTIVE; 755 686 if (ebr) { // encoding reference to EBR 756 687 trgt->ptype = PT_EXTENDED_LBA; -
uspace/lib/mbr/libmbr.h
rc9f61150 r9bda5d90 1 1 /* 2 * Copyright (c) 2009 Jiri Svoboda3 2 * Copyright (c) 2011, 2012, 2013 Dominik Taborsky 4 3 * All rights reserved. … … 74 73 75 74 typedef enum { 76 /** Other flags unknown - saving previous state */77 75 /** Bootability */ 78 ST_BOOT = 7,76 ST_BOOT = 0, 79 77 /** Logical partition, 0 = primary, 1 = logical*/ 80 ST_LOGIC = 878 ST_LOGIC = 1 81 79 } MBR_FLAGS; 82 80 … … 103 101 /** Partition overlapping */ 104 102 ERR_OVERLAP, 105 /** Partition out of bounds */103 /** Logical partition out of bounds */ 106 104 ERR_OUT_BOUNDS, 107 105 /** No space left for EBR */ … … 109 107 /** Out of memory */ 110 108 ERR_NOMEM, 111 /** Libblock error */ 112 ERR_LIBBLOCK, 113 } mbr_err_val; 109 } MBR_ERR_VAL; 114 110 115 111 … … 147 143 /** Raw access to data */ 148 144 br_block_t raw_data; 145 /** Device where the data are from */ 146 service_id_t device; 149 147 } mbr_t; 150 148 … … 157 155 uint8_t type; 158 156 /** Flags */ 159 uint 16_t status;157 uint8_t status; 160 158 /** Address of first block */ 161 159 uint32_t start_addr; … … 181 179 182 180 /** Both header and partition list */ 183 typedef struct mbr_label { 184 /** MBR header */ 181 typedef struct mbr_table { 185 182 mbr_t * mbr; 186 /** Partition list */187 183 mbr_partitions_t * parts; 188 /** Device where the data are from (or for) */ 189 service_id_t device; 190 } mbr_label_t; 191 192 /* Alloc complete label structure */ 193 extern mbr_label_t * mbr_alloc_label(void); 194 195 /* Read/Write MBR header. 184 } mbr_table_t; 185 186 /** Read/Write MBR header. 196 187 * WARNING: when changing both header and partitions, write first header, 197 188 * then partitions. The MBR headers' raw_data is NOT updated to follow 198 189 * partition changes. */ 199 190 extern mbr_t * mbr_alloc_mbr(void); 200 extern int mbr_read_mbr(mbr_label_t *, service_id_t);201 extern int mbr_write_mbr(mbr_ label_t *, service_id_t);202 extern int mbr_is_mbr(mbr_ label_t *);203 204 /* Read/Write/Set MBR partitions.191 extern mbr_t * mbr_read_mbr(service_id_t dev_handle); 192 extern int mbr_write_mbr(mbr_t * mbr, service_id_t dev_handle); 193 extern int mbr_is_mbr(mbr_t * mbr); 194 195 /** Read/Write/Set MBR partitions. 205 196 * NOTE: Writing partitions writes the complete header as well. */ 206 extern int mbr_read_partitions(mbr_label_t *);207 extern int mbr_write_partitions(mbr_label_t *, service_id_t);208 extern mbr_part_t * 197 extern mbr_partitions_t * mbr_read_partitions(mbr_t * mbr); 198 extern int mbr_write_partitions(mbr_partitions_t * parts, mbr_t * mbr, service_id_t dev_handle); 199 extern mbr_part_t * mbr_alloc_partition(void); 209 200 extern mbr_partitions_t * mbr_alloc_partitions(void); 210 extern mbr_err_val mbr_add_partition(mbr_label_t *, mbr_part_t *);211 extern int mbr_remove_partition(mbr_label_t *, size_t);212 extern int mbr_get_flag(mbr_part_t *, MBR_FLAGS);213 extern void mbr_set_flag(mbr_part_t *, MBR_FLAGS, bool);214 extern uint32_t mbr_get_next_aligned(uint32_t, unsigned int);201 extern MBR_ERR_VAL mbr_add_partition(mbr_partitions_t * parts, mbr_part_t * partition); 202 extern int mbr_remove_partition(mbr_partitions_t * parts, size_t idx); 203 extern int mbr_get_flag(mbr_part_t * p, MBR_FLAGS flag); 204 extern void mbr_set_flag(mbr_part_t * p, MBR_FLAGS flag, bool value); 205 extern uint32_t mbr_get_next_aligned(uint32_t addr, unsigned int alignment); 215 206 216 207 #define mbr_part_foreach(parts, iterator) \ 217 for (iterator = list_get_instance((parts)->list.head.next, mbr_part_t, link); \ 218 iterator != list_get_instance(&((parts)->list.head), mbr_part_t, link); \ 219 iterator = list_get_instance(iterator->link.next, mbr_part_t, link)) 220 221 222 /* free() wrapper functions. */ 223 extern void mbr_free_label(mbr_label_t *); 224 extern void mbr_free_mbr(mbr_t *); 225 extern void mbr_free_partition(mbr_part_t *); 226 extern void mbr_free_partitions(mbr_partitions_t *); 208 for (iterator = list_get_instance((parts)->list.head.next, mbr_part_t, link); \ 209 iterator != list_get_instance(&((parts)->list.head), mbr_part_t, link); \ 210 iterator = list_get_instance(iterator->link.next, mbr_part_t, link)) 211 212 213 /** free() wrapper functions. */ 214 extern void mbr_free_mbr(mbr_t * mbr); 215 extern void mbr_free_partition(mbr_part_t * p); 216 extern void mbr_free_partitions(mbr_partitions_t * parts); 227 217 228 218 #endif
Note:
See TracChangeset
for help on using the changeset viewer.