Changeset 6453e306 in mainline for uspace/app/hdisk/func_mbr.c


Ignore:
Timestamp:
2013-12-25T16:09:43Z (11 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
ac36aed
Parents:
d51beba3
Message:

basic code review and coding style cleanup

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/hdisk/func_mbr.c

    rd51beba3 r6453e306  
    11/*
    2  * Copyright (c) 2012, 2013 Dominik Taborsky
     2 * Copyright (c) 2012-2013 Dominik Taborsky
    33 * All rights reserved.
    44 *
     
    2727 */
    2828
    29  /** @addtogroup hdisk
     29/** @addtogroup hdisk
    3030 * @{
    3131 */
     
    3737#include <str_error.h>
    3838#include <sys/types.h>
    39 
    4039#include "func_mbr.h"
    4140#include "input.h"
     
    6261int add_mbr_part(label_t *this, tinput_t *in)
    6362{
    64         int rc;
    65        
    66         mbr_part_t *part = mbr_alloc_partition();
    67        
    68         rc = set_mbr_partition(in, part, this);
     63        mbr_part_t *partition = mbr_alloc_partition();
     64        if (partition == NULL)
     65                return ENOMEM;
     66       
     67        int rc = set_mbr_partition(in, partition, this);
    6968        if (rc != EOK)
    7069                return rc;
    7170       
    72         rc = mbr_add_partition(this->data.mbr, part);
    73         if (rc != ERR_OK) {
     71        rc = mbr_add_partition(this->data.mbr, partition);
     72        if (rc != ERR_OK)
    7473                printf("Error adding partition: %d\n", rc);
    75         }
    7674       
    7775        return EOK;
     
    8078int delete_mbr_part(label_t *this, tinput_t *in)
    8179{
    82         int rc;
    83         size_t idx;
    84        
    85         printf("Number of the partition to delete (counted from 0): ");
    86         idx = get_input_size_t(in);
    87        
    88         if (idx == 0 && errno != EOK)
    89                 return errno;
    90        
    91         rc = mbr_remove_partition(this->data.mbr, idx);
    92         if (rc != EOK) {
     80        printf("Index of the partition to delete (counted from 0): ");
     81        size_t idx = get_input_size_t(in);
     82       
     83        if ((idx == 0) && (errno != EOK))
     84                return errno;
     85       
     86        int rc = mbr_remove_partition(this->data.mbr, idx);
     87        if (rc != EOK)
    9388                printf("Error: partition does not exist?\n");
    94         }
    9589       
    9690        return EOK;
     
    113107}
    114108
    115 /** Print current partition scheme */
    116109int print_mbr_parts(label_t *this)
    117110{
    118         int num = 0;
    119        
    120         printf("Current partition scheme (MBR)(number of blocks: %" PRIu64 "):\n", this->nblocks);
    121         printf("\t\t%10s  %10s %10s %10s %7s\n", "Bootable:", "Start:", "End:", "Length:", "Type:");
    122        
     111        printf("Current partition scheme: MBR\n");
     112        printf("Number of blocks: %" PRIu64 "\n", this->blocks);
     113        printf("\t\t%10s  %10s %10s %10s %7s\n",
     114            "Bootable:", "Start:", "End:", "Length:", "Type:");
     115       
     116        unsigned int num = 0;
    123117        mbr_part_t *it;
    124        
    125118        for (it = mbr_get_first_partition(this->data.mbr); it != NULL;
    126119             it = mbr_get_next_partition(this->data.mbr, it)) {
     
    128121                        continue;
    129122               
    130                 printf("\tP%d:\t", num);
     123                printf("\tP%u:\t", num);
    131124                if (mbr_get_flag(it, ST_BOOT))
    132125                        printf("*");
     
    134127                        printf(" ");
    135128               
    136                 printf("\t%10u %10u %10u %7u\n", it->start_addr, it->start_addr + it->length, it->length, it->type);
     129                printf("\t%10u %10u %10u %7u\n", it->start_addr,
     130                    it->start_addr + it->length, it->length, it->type);
    137131               
    138132                num++;
    139133        }
    140134       
    141         printf("%d partitions found.\n", num);
     135        printf("%u partitions found.\n", num);
    142136       
    143137        return EOK;
     
    146140int read_mbr_parts(label_t *this)
    147141{
    148         int rc;
    149         rc = mbr_read_mbr(this->data.mbr, this->device);
     142        int rc = mbr_read_mbr(this->data.mbr, this->device);
    150143        if (rc != EOK)
    151144                return rc;
     
    164157{
    165158        int rc = mbr_write_partitions(this->data.mbr, this->device);
    166         if (rc != EOK) {
    167                 printf("Error occured during writing: ERR: %d: %s\n", rc, str_error(rc));
    168         }
     159        if (rc != EOK)
     160                printf("Error occured during writing: ERR: %d: %s\n", rc,
     161                    str_error(rc));
    169162       
    170163        return rc;
     
    177170}
    178171
    179 static int set_mbr_partition(tinput_t *in, mbr_part_t *p, label_t * this)
    180 {
    181         int c;
    182         uint8_t type;
    183        
     172static int set_mbr_partition(tinput_t *in, mbr_part_t *partition, label_t *this)
     173{
    184174        printf("Primary (p) or logical (l): ");
    185         c = getchar();
     175        int c = getchar();
    186176        printf("%c\n", c);
    187177
    188178        switch (c) {
    189179        case 'p':
    190                 mbr_set_flag(p, ST_LOGIC, false);
     180                mbr_set_flag(partition, ST_LOGIC, false);
    191181                break;
    192182        case 'l':
    193                 mbr_set_flag(p, ST_LOGIC, true);
     183                mbr_set_flag(partition, ST_LOGIC, true);
    194184                break;
    195185        default:
     
    198188        }
    199189       
    200         printf("Set type (0-255): ");
    201         type = get_input_uint8(in);
    202         if (type == 0 && errno != EOK)
    203                 return errno;
    204 
    205         ///TODO: there can only be one boolabel partition; let's do it just like fdisk
     190        printf("Set type (0 - 255): ");
     191        uint8_t type = get_input_uint8(in);
     192        if ((type == 0) && (errno != EOK))
     193                return errno;
     194       
     195        // FIXME: Make sure there is at most one bootable partition
    206196        printf("Bootable? (y/n): ");
    207197        c = getchar();
    208         if (c != 'y' && c != 'Y' && c != 'n' && c != 'N') {
     198        if ((c != 'y') && (c != 'Y') && (c != 'n') && (c != 'N')) {
    209199                printf("Invalid value. Cancelled.");
    210200                return EINVAL;
    211201        }
     202       
    212203        printf("%c\n", c);
    213         mbr_set_flag(p, ST_BOOT, (c == 'y' || c == 'Y') ? true : false);
    214 
    215 
    216         uint32_t sa, ea;
    217 
     204        mbr_set_flag(partition, ST_BOOT, (c == 'y' || c == 'Y') ? true : false);
     205       
    218206        printf("Set starting address: ");
    219         sa = get_input_uint32(in);
    220         if (sa == 0 && errno != EOK)
    221                 return errno;
    222        
    223         if (this->alignment != 0 && this->alignment != 1 && sa % this->alignment != 0) {
     207        uint32_t sa = get_input_uint32(in);
     208        if ((sa == 0) && (errno != EOK))
     209                return errno;
     210       
     211        if ((this->alignment != 0) && (this->alignment != 1) &&
     212            (sa % this->alignment != 0)) {
    224213                sa = mbr_get_next_aligned(sa, this->alignment);
    225                 printf("Starting address was aligned to %u.\n", sa);
    226         }
    227        
    228         printf("Set end addres (max: %" PRIu64 "): ", this->nblocks);
    229         ea = get_input_uint32(in);
    230         if (ea == 0 && errno != EOK)
     214                printf("Starting address was aligned to %" PRIu32 ".\n", sa);
     215        }
     216       
     217        printf("Set end addres (max: %" PRIu64 "): ", this->blocks);
     218        uint32_t ea = get_input_uint32(in);
     219        if ((ea == 0) && (errno != EOK))
    231220                return errno;
    232221       
     
    236225        }
    237226       
    238         p->type = type;
    239         p->start_addr = sa;
    240         p->length = ea - sa;
    241 
    242         return EOK;
    243 }
    244 
    245 
    246 
    247 
    248 
    249 
     227        partition->type = type;
     228        partition->start_addr = sa;
     229        partition->length = ea - sa;
     230       
     231        return EOK;
     232}
Note: See TracChangeset for help on using the changeset viewer.