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