免费注册 查看新帖 |

Chinaunix

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

kernle 2.6 build.c analysis [复制链接]

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

                /*
*  $Id: build.c,v 1.5 1997/05/19 12:29:58 mj Exp $
*
*  Copyright (C) 1991, 1992  Linus Torvalds
*  Copyright (C) 1997 Martin Mares
*/
/*
* This file builds a disk-image from three different files:
*
* - bootsect: exactly 512 bytes of 8086 machine code, loads the rest
* - setup: 8086 machine code, sets up system parm
* - system: 80386 code for actual system
*
* It does some checking that all files are of the correct type, and
* just writes the result to stdout, removing headers and padding to
* the right amount. It also writes some system data to stderr.
*/
/*
* Changes by tytso to allow root device specification
* High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
* Cross compiling fixes by Gertjan van Wingerde, July 1996
* Rewritten by Martin Mares, April 1997
*/
#include      /*this file was complied to generate execute file build*/
#include     /* which was used to construct the bzImage*/
#include     /* all the headers are stand userspace`s header files*/
#include
#include
#include
#include
#include
#include
#include
typedef unsigned char byte;
typedef unsigned short word;
typedef unsigned long u32;
#define DEFAULT_MAJOR_ROOT 0
#define DEFAULT_MINOR_ROOT 0
/* Minimal number of setup sectors (see also bootsect.S) */
#define SETUP_SECTS 4
byte buf[1024];
int fd;
int is_big_kernel;
void die(const char * str, ...)
{
    va_list args;
    va_start(args, str);
    vfprintf(stderr, str, args);
    fputc('\n', stderr);
    exit(1);
}
void file_open(const char *name)
{
    if ((fd = open(name, O_RDONLY, 0))  image]");
}
int main(int argc, char ** argv)
{
    unsigned int i, c, sz, setup_sectors;
    u32 sys_size;
    byte major_root, minor_root;
    struct stat sb;
    if (argc > 2 && !strcmp(argv[1], "-b"))
      {
        is_big_kernel = 1;
        argc--, argv++;
      }
    if ((argc  5))
        usage();
    if (argc > 4) {
        if (!strcmp(argv[4], "CURRENT")) {
            if (stat("/", &sb)) {
                perror("/");
                die("Couldn't stat /");
            }
            major_root = major(sb.st_dev);
            minor_root = minor(sb.st_dev);
        } else if (strcmp(argv[4], "FLOPPY")) {
            if (stat(argv[4], &sb)) {
                perror(argv[4]);
                die("Couldn't stat root device.");
            }
            major_root = major(sb.st_rdev);
            minor_root = minor(sb.st_rdev);
        } else {
            major_root = 0;
            minor_root = 0;
        }
    } else {
        major_root = DEFAULT_MAJOR_ROOT;
        minor_root = DEFAULT_MINOR_ROOT;
    }
    fprintf(stderr, "Root device is (%d, %d)\n", major_root, minor_root);
    file_open(argv[1]);
    i = read(fd, buf, sizeof(buf));
    fprintf(stderr,"Boot sector %d bytes.\n",i);
    if (i != 512)
        die("Boot block must be exactly 512 bytes");
    if (buf[510] != 0x55 || buf[511] != 0xaa)
        die("Boot block hasn't got boot flag (0xAA55)");
    buf[508] = minor_root;
    buf[509] = major_root;
    if (write(1, buf, 512) != 512)
        die("Write call failed");
    close (fd);
    file_open(argv[2]);      
             /* Copy
the setup code */
    for (i=0 ; (c=read(fd, buf, sizeof(buf)))>0 ; i+=c )
        if (write(1, buf, c) != c)
            die("Write call failed");
    if (c != 0)
        die("read-error on `setup'");
    close (fd);
    setup_sectors = (i + 511) / 512;    /* Pad unused space with zeros */
    /* for compatibility with ancient versions of LILO. */
    if (setup_sectors  sizeof(buf))
            c = sizeof(buf);
        if (write(1, buf, c) != c)
            die("Write call failed");
        i += c;
    }
    file_open(argv[3]);
    if (fstat (fd, &sb))
        die("Unable to stat `%s': %m", argv[3]);
    sz = sb.st_size;
    fprintf (stderr, "System is %d kB\n", sz/1024);
    sys_size = (sz + 15) / 16;
    if (!is_big_kernel && sys_size > DEF_SYSSIZE)
        die("System is too big. Try using bzImage or modules.");
    while (sz > 0) {
        int l, n;
        l = (sz > sizeof(buf)) ? sizeof(buf) : sz;
        if ((n=read(fd, buf, l)) != l) {
            if (n > 8) & 0xff);
    if (write(1, buf, 2) != 2)
        die("Write of image length failed");
    return 0;      
           
     /* Everything is OK */
}
用可执行文件build创建bzImage时的命令如下 :
arch/i386/boot/tools/build \
    -b arch/i386/boot/bootsect \
    arch/i386/boot/setup \
    arch/i386/boot/vmlinux.bin\
    CURRENT \
    > arch/i386/boot/bzImage
其功能为:将boosect,setup,vmlinux.bin三个文件依次顺序的存放到bzImage文件中去,
并修改boosect部分内容,以及保证setup占用整数个sector,
,然后将 mlinux.bin追加到文件之后
步驟如下:
1:判断bootsect是否为有效的引导扇区(bzImage[508,509]={minor_root,major_root]);
2:填充setup部分,将不足一个扇区的部分用0填充;
3:复制vmlinux.bin到setup后面即可;
4:按照setup,vmlinux.bin实际的所占扇区,及大小更改boosect中相应的指示位置
    bzImage[497]=bootsect;
    bzImage[500,501]=sys_size;
               
               
               
               
               

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

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP