📄 build.c
字号:
#include <stdio.h> /* fprintf */
#include <stdlib.h> /* contains exit */
#include <sys/types.h> /* unistd.h needs this */
#include <unistd.h> /* contains read/write */
#include <fcntl.h>
#define MINIX_HEADER 32
#define GCC_HEADER 1024
void die(char * str)
{
fprintf(stderr,"%s\n",str);
exit(1);
}
void usage(void)
{
die("Usage: build boot system [> image]");
}
int main(int argc, char ** argv)
{
int i,c,id;
char buf[1024];
if (argc != 3)
usage();
for (i=0;i<sizeof buf; i++) buf[i]=0;
//以下的几个IF语句是判断BOOT文件是否是MINIX文件头
if ((id=open(argv[1],O_RDONLY,0))<0)
die("Unable to open 'boot'");
if (read(id,buf,MINIX_HEADER) != MINIX_HEADER)
die("Unable to read header of 'boot'");
if (((long *) buf)[0]!=0x04100301)
die("Non-Minix header of 'boot'");
if (((long *) buf)[1]!=MINIX_HEADER)
die("Non-Minix header of 'boot'");
if (((long *) buf)[3]!=0)
die("Illegal data segment in 'boot'");
if (((long *) buf)[4]!=0)
die("Illegal bss in 'boot'");
if (((long *) buf)[5] != 0)
die("Non-Minix header of 'boot'");
if (((long *) buf)[7] != 0)
die("Illegal symbol table in 'boot'");
i=read(id,buf,sizeof buf);
fprintf(stderr,"Boot sector %d bytes.\n",i);
if (i>510) //引导程序一定要小于510字节
die("Boot block may not exceed 510 bytes");
//以下二行表示的是正确的引导标识
buf[510]=0x55;
buf[511]=0xAA;
i=write(1,buf,512); //写引导程序
if (i!=512)
die("Write call failed");
close (id);
if ((id=open(argv[2],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'");
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 %d bytes.\n",i);
return(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -