/*
* linux/tools/build.c
*
* (C) 1991 Linus Torvalds
*/
/*
* This file builds a disk-image from three different files:
*
* - bootsect: max 510 bytes of 8086 machine code, loads the rest
* - setup: max 4 sectors of 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.
*/
/*
* 该程序从三个不同的程序中创建磁盘映象文件:
*
* - bootsect:该文件的8086 机器码最长为510 字节,用于加载其它程序。
* - setup:该文件的8086 机器码最长为4 个磁盘扇区,用于设置系统参数。
* - system:实际系统的80386 代码。
*
* 该程序首先检查所有程序模块的类型是否正确,并将检查结果在终端上显示出来,
* 然后删除模块头部并扩充大正确的长度。该程序也会将一些系统数据写到stderr。
*/
/*
* Changes by tytso to allow root device specification
*/
/*
* tytso 对该程序作了修改,以允许指定根文件设备。
*/
#include <stdio.h> /* fprintf */
#include <string.h>
#include <stdlib.h> /* contains exit */
#include <sys/types.h> /* unistd.h needs this */
#include <sys/stat.h>
#include <linux/fs.h>
#include <unistd.h> /* contains read/write */
#include <fcntl.h>
#define MINIX_HEADER 32
#define GCC_HEADER 1024
#define SYS_SIZE 0x3000
#define DEFAULT_MAJOR_ROOT 3
#define DEFAULT_MINOR_ROOT 6
/* max nr of sectors of setup: don't change unless you also change
* bootsect etc */
/* 下面指定setup 模块占的最大扇区数:不要改变该值,除非也改变bootsect 等相应文件。*/
#define SETUP_SECTS 4
#define STRINGIFY(x) #x
void die(char * str)
{
fprintf(stderr,"%s/n",str);
exit(1);
}
void usage(void)
{
die("Usage: build bootsect setup system [rootdev] [> image]");
}
int main(int argc, char ** argv)
{
int i,c,id;
char buf[1024];
char major_root, minor_root;
struct stat sb;
if ((argc != 4) && (argc != 5))
usage();
/* 如果参数是5 个,则说明带有根设备名。*/
if (argc == 5) {
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 {/* 若参数只有4 个,则让主设备号和次设备号等于系统默认的根设备。*/
major_root = DEFAULT_MAJOR_ROOT;
minor_root = DEFAULT_MINOR_ROOT;
}
fprintf(stderr, "Root device is (%d, %d)/n", major_root, minor_root);
if ((major_root != 2) && (major_root != 3) &&
(major_root != 0)) {
fprintf(stderr, "Illegal root device (major = %d)/n",
major_root);
die("Bad root device --- major #");
}
for (i=0;i<sizeof buf; i++) buf[i]=0;
/* 以只读方式打开参数1 指定的文件(bootsect),若出错则显示出错信息,退出。*/
if ((id=open(argv[1],O_RDONLY,0))<0)
die("Unable to open 'boot'");
/* 读取文件中的minix 执行头部信息(参见列表后说明),若出错则显示出错信息,退出。*/
if (read(id,buf,MINIX_HEADER) != MINIX_HEADER)
die("Unable to read header of 'boot'");
/* 0x0301 - minix 头部a_magic 魔数;0x10 - a_flag 可执行;0x04 - a_cpu, Intel 8086 机器码。*/
if (((long *) buf)[0]!=0x04100301)
die("Non-Minix header of 'boot'");
/* 判断头部长度字段a_hdrlen(字节)是否正确。(后三字节正好没有用,是0)*/
if (((long *) buf)[1]!=MINIX_HEADER)
die("Non-Minix header of 'boot'");
/* 判断数据段长a_data 字段(long)内容是否为0。*/
if (((long *) buf)[3]!=0)
die("Illegal data segment in 'boot'");
/* 判断堆a_bss 字段(long)内容是否为0。*/
if (((long *) buf)[4]!=0)
die("Illegal bss in 'boot'");
/* 判断执行点a_entry 字段(long)内容是否为0。*/
if (((long *) buf)[5] != 0)
die("Non-Minix header of 'boot'");
/* 判断符号表长字段a_sym 的内容是否为0。*/
if (((long *) buf)[7] != 0)
die("Illegal symbol table in 'boot'");
/* 读取实际代码数据,应该返回读取字节数为512 字节。*/
i=read(id,buf,sizeof buf);
fprintf(stderr,"Boot sector %d bytes./n",i);
if (i != 512)
die("Boot block must be exactly 512 bytes");
/* 判断boot 块0x510 处是否有可引导标志0xAA55。*/
if ((*(unsigned short *)(buf+510)) != 0xAA55)
die("Boot block hasn't got boot flag (0xAA55)");
/* 引导块的508,509 偏移处存放的是根设备号。*/
buf[508] = (char) minor_root;
buf[509] = (char) major_root;
/* 将该boot 块512 字节的数据写到标准输出stdout,若写出字节数不对,则显示出错信息,退出。*/
i=write(1,buf,512);
if (i!=512)
die("Write call failed");
/* 最后关闭bootsect 模块文件。*/
close (id);
/* 现在开始处理setup 模块。首先以只读方式打开该模块,若出错则显示出错信息,退出。*/
if ((id=open(argv[2],O_RDONLY,0))<0)
die("Unable to open 'setup'");
if (read(id,buf,MINIX_HEADER) != MINIX_HEADER)
die("Unable to read header of 'setup'");
if (((long *) buf)[0]!=0x04100301)
die("Non-Minix header of 'setup'");
if (((long *) buf)[1]!=MINIX_HEADER)
die("Non-Minix header of 'setup'");
if (((long *) buf)[3]!=0)
die("Illegal data segment in 'setup'");
if (((long *) buf)[4]!=0)
die("Illegal bss in 'setup'");
if (((long *) buf)[5] != 0)
die("Non-Minix header of 'setup'");
if (((long *) buf)[7] != 0)
die("Illegal symbol table in 'setup'");
for (i=0 ; (c=read(id,buf,sizeof buf))>0 ; i+=c )
if (write(1,buf,c)!=c)
die("Write call failed");
close (id);
/* 若setup 模块长度大于4 个扇区,则算出错,显示出错信息,退出。*/
if (i > SETUP_SECTS*512)
die("Setup exceeds " STRINGIFY(SETUP_SECTS)
" sectors - rewrite build/boot/setup");
fprintf(stderr,"Setup is %d bytes./n",i);
/* 将缓冲区buf 清零。*/
for (c=0 ; c<sizeof(buf) ; c++)
buf[c] = '/0';
/* 若setup 长度小于4*512 字节,则用/0 将setup 填足为4*512 字节。*/
while (i<SETUP_SECTS*512) {
c = SETUP_SECTS*512-i;
if (c > sizeof(buf))
c = sizeof(buf);
if (write(1,buf,c) != c)
die("Write call failed");
i += c;
}
/* 下面处理system 模块。首先以只读方式打开该文件。*/
if ((id=open(argv[3],O_RDONLY,0))<0)
die("Unable to open 'system'");
// if (read(id,buf,GCC_HEADER) != GCC_HEADER)
// die("Unable to read header of 'system'");
// if (((long *) buf)[5] != 0)
// die("Non-GCC header of 'system'");
/* 读取随后的执行代码数据,并写到标准输出stdout。*/
for (i=0 ; (c=read(id,buf,sizeof buf))>0 ; i+=c )
if (write(1,buf,c)!=c)
die("Write call failed");
close(id);
fprintf(stderr,"System is %d bytes./n",i);
/* 若system 代码数据长度超过SYS_SIZE 节(或128KB 字节),则显示出错信息,退出。*/
if (i > SYS_SIZE*16)
die("System is too big");
return(0);
}