免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1804 | 回复: 0
打印 上一主题 下一主题

OpenBSD原代码导读之sbin/fdisk.c [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-02-02 14:37 |只看该作者 |倒序浏览

/*    $OpenBSD: fdisk.c,v 1.46 2006/11/09 00:01:10 deraadt Exp $    */
/*
* Copyright (c) 1997 Tobias Weingartner
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include err.h>
#include stdio.h>
#include stdlib.h>
#include string.h>
#include unistd.h>
#include paths.h>
#include sys/types.h>
#include sys/fcntl.h>
#include sys/disklabel.h>
#include "disk.h"
#include "user.h"
#define _PATH_MBR _PATH_BOOTDIR "mbr"
两个宏一起定义,呵呵,以前从来没有这样写过代码
static unsigned char builtin_mbr[] = {
#include "mbrcode.h"
};
这个地方的用法很生僻啊,把一个数组的内容放在一个文件mbrcode.h中,这样写的优点:
以后mbr的代码改了,只要修改mbrcode.h就可以了,不需要改c文件。同时这个mbrcode.h文件估计也是由程序自动生成的。
int    y_flag; 对应fdisk命令的-y参数,y参数的解释是do not ask question。这个变量程序中这个变量没有起作用,估计是为了兼容旧版本
usage这个函数打印fdisk命令的用法
static void
usage(void)
{
    extern char * __progname;
    fprintf(stderr, "usage: %s "
     "[-ieuy] [-c cylinders -h heads -s sectors] [-f mbrfile] device\n"
     "\t-i: initialize disk with virgin MBR\n"
     "\t-u: update MBR code, preserve partition table\n"
     "\t-e: edit MBRs on disk interactively\n"
     "\t-f: specify non-standard MBR template\n"
     "\t-chs: specify disk geometry\n"
     "\t-y: do not ask questions\n"
     "`disk' may be of the forms: sd0 or /dev/rsd0c.\n",
     __progname);
    exit(1);
}
int
main(int argc, char *argv[])
{
    int ch, fd;
    int i_flag = 0, m_flag = 0, u_flag = 0;
    int c_arg = 0, h_arg = 0, s_arg = 0;
    disk_t disk;
    DISK_metrics *usermetrics;
#if defined(__amd64__) || defined(__i386__) || defined (__powerpc__) || \
    defined(__sh__)
    char *mbrfile = _PATH_MBR;
    从上面的代码可以看出,主引导记录这种东西,只对__amd64,__i386__, __powerpc__这几种体系的CPU适用,别的体系的CPU可能并不需要这玩意。
#else
    char *mbrfile = NULL;
#endif
    mbr_t mbr;
    char mbr_buf[DEV_BSIZE]; DEV_BSIZE=512,这个定义在文件mbr.h中,原定义如下:
/* Sanity(明智,轻醒) check */
#include
#if (DEV_BSIZE != 512)
#error "DEV_BSIZE != 512, somebody better fix me!"
#endif
一个磁盘的扇区大小恰好为512字节,所以这里的mbr_buf就是用于读写主引导扇区的。

    下面的代码用于检测fdisk命令输入的参数,设置相应的标志
     while ((ch = getopt(argc, argv, "ieuf:c:h:s:")) != -1) {
        const char *errstr;
        switch(ch) {
        case 'i':初始化磁盘的MBR引导扇区的标志,一般用于初始化一块新硬盘
            i_flag = 1;
            break;
        case 'u': Update硬盘的MBR,并且打印出分区表的标志
            u_flag = 1;
            break;
        case 'e': 进入交互模式,手工编辑磁盘的引导扇区信息
            m_flag = 1;
            break;
        case 'f': 指定mbr主引导记录代码的位置,否则使用默认的引导代码
            mbrfile = optarg;
            break;
        case 'c': 指定磁盘的柱面参数
            c_arg = strtonum(optarg, 1, 262144, &errstr);
            if (errstr)
                errx(1, "Cylinder argument %s [1..262144].",
                 errstr);
            break;
        case 'h': 指定磁盘的磁头参数
            h_arg = strtonum(optarg, 1, 256, &errstr);
            if (errstr)
                errx(1, "Head argument %s [1..256].", errstr);
            break;
        case 's': 指定磁盘的扇区参数
            s_arg = strtonum(optarg, 1, 63, &errstr);
            if (errstr)
                errx(1, "Sector argument %s [1..63].", errstr);
            break;
        default:
            usage();
        }
    }
    argc -= optind; 这里做减法算出还有多少个参数没有处理
    argv += optind;

    这里应该还剩下一个参数,表示要格式的磁盘的名字,如果为空,则打印fdisk的使用方法。
    /* Argument checking */
    if (argc != 1)
        usage();    else
        disk.name = argv[0];
    下面的代码判断是不是自己指定了硬盘的CHS信息,如果是的话,则设置相应的数据结构,

估计现在fdisk的时候,没人会吃饱了没事干自己指定CHS信息。
    /* Put in supplied geometry if there */
    if (c_arg | h_arg | s_arg) {
        usermetrics = malloc(sizeof(DISK_metrics));
        if (usermetrics != NULL) {
            if (c_arg && h_arg && s_arg) {
                usermetrics->cylinders = c_arg;
                usermetrics->heads = h_arg;
                usermetrics->sectors = s_arg;
                usermetrics->size = c_arg * h_arg * s_arg;
            } else
                errx(1, "Please specify a full geometry with [-chs].");
        }
    } else
        usermetrics = NULL;
    下面的调用用于获取磁盘的信息,DISK_getmetrics在disk.c文件中实现的,将在分析disk.c的时候详细说明
    /* Get the geometry */
    disk.real = NULL;
    if (DISK_getmetrics(&disk, usermetrics))
        errx(1, "Can't get disk geometry, please use [-chs] to specify.");
   
    如果没有i, u, m标志的话,则打印磁盘的信息后退出
    /* Print out current MBRs on disk */
    if ((i_flag + u_flag + m_flag) == 0)
        exit(USER_print_disk(&disk));
    /* Parse mbr template, to pass on later */
   
    if (mbrfile != NULL && (fd = open(mbrfile, O_RDONLY)) == -1) {
        warn("%s", mbrfile);
        warnx("using builtin MBR");
        mbrfile == NULL;
    }
    if (mbrfile == NULL) {
        memcpy(mbr_buf, builtin_mbr, sizeof(mbr_buf));
    } else {
        MBR_read(fd, 0, mbr_buf);
        close(fd);
    }
    上面的代码把主引导文件的内容拷贝到数组mbr_buf中,接着调用MBR_parse,分析MBR的内容,这个函数是在mbr.c中实现的,将在mbr.c文件中分析
    MBR_parse(&disk, mbr_buf, 0, 0, &mbr);
    /* Now do what we are supposed to */
    如果制定了-i或-u标志,则初始化磁盘的主引导记录,在USER_init中实现
    if (i_flag || u_flag)
        if (USER_init(&disk, &mbr, u_flag) == -1)
            err(1, "error initializing MBR");
   
    如果指定了-e标志,则进入交互模式,手工设置磁盘主引导记录的信息,
    这个函数在文件user.c文件中实现
    if (m_flag)
        USER_modify(&disk, &mbr, 0, 0);

从上面的代码中可以看出,OpenBSD下的fdisk命令用法:
fdisk "[-ieuy] [-c cylinders -h heads -s sectors] [-f mbrfile] device"
参数:-i --->初始化磁盘的主引导记录MBR
       -e --->进入交互模式修改MBR的信息
       -u --->更新磁盘的主引导记录MBR
       -y --->没啥作用
       -c 柱面号 -h 磁头号 -s 扇区号
       -f mbrfile --->指定MBR文件的位置
       device 磁盘的名称,对于OpenBSD来说,为wd0, wd1, wd2...,依次类推。


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/31277/showart_241983.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP