📄 mkhd.cpp
字号:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<fstream>
#define TOTSEC 20160
#define IMGSZ TOTSEC*512
#define PARTSEC (TOTSEC - 63)
#define FATENT (PARTSEC/8)
#define EOC 0x0FFFFFFF
#define FAIL(file) do{\
printf("read failed :%s\n",file);\
exit(0);\
}while(0)
typedef unsigned char byte;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef struct
{
byte dir_name[11];
byte dir_attr;
byte dir_NTres;
byte crt_time_tenth;
ushort dir_crt_time;
ushort dir_crt_date;
ushort dir_lst_acc_date;
ushort dir_fst_clusHI;
ushort dir_wrt_time;
ushort dir_wrt_date;
ushort dir_fst_clusLO;
uint dir_file_size;
}fdir;
void openfile(std::ifstream &i,char *name,unsigned *sz)
{
i.open(name,std::ios::binary);
if(!i)
{
printf("%s failed\n",name);
exit(0);
}
if(sz)
{
i.seekg(0,std::ios::end);
*sz = i.tellg();
printf("%s size : %d\n",name,*sz);
i.seekg(0,std::ios::beg);
}
}
void readfile(std::ifstream &i,char *buf,int sz)
{
i.read(buf,sz);
if(!i.gcount())
{
printf("read failed\n");
exit(0);
}
}
int main()
{
char *buf = new char[IMGSZ];
char *obuf = buf;
if(!buf)
{
printf("buffer failed\n");
exit(0);
}
std::ifstream i;
unsigned s1,s2,s3,s4,s5,s6,s7;
openfile(i,"mbr.com",&s1);
readfile(i,buf,s1);
i.close();
buf += 63*512;
uint t = (uint)buf + 32*512;
openfile(i,"boot1.com",&s2);
readfile(i,buf,s2);
i.close();
buf += s2;
openfile(i,"boot2.com",&s3);
readfile(i,buf,s3);
i.close();
buf += s3;
openfile(i,"load.com",&s4);
readfile(i,buf,s4);
i.close();
buf += s4;
buf = (char*)t;
uint fatsec = (FATENT*4)/512;
if((FATENT*4)%512) fatsec++;
t = (uint)buf + fatsec*512;
uint *fat = (uint*)buf;
fat[0] = 0xFFFFFFFF;
fat[1] = 0xFFFFFFFF;
int j;
for(j=2;j<FATENT;j++)
{
fat[j] = 0;
}
buf =(char*)t;
fdir *dir = (fdir*)buf;
strncpy((char*)dir->dir_name,"KER COM",11);
dir->dir_fst_clusHI = 0;
dir->dir_fst_clusLO = 3;
dir->dir_file_size = s5;
fat[2] = EOC;
t = (uint)buf + 4096;
buf += sizeof(fdir);
dir = (fdir*)buf;
buf += sizeof(fdir);
fdir *dirs = (fdir*)buf;
buf = (char*)t;
openfile(i,"ker.com",&s5);
readfile(i,buf,s5);
i.close();
j = 3;
for(int ks=0;ks<s5;ks+=4096)
{
fat[j] = j+1;
j++;
buf += 4096;
}
fat[j-1] = EOC;
printf("kernel end clus: %d\n",j-1);
openfile(i,"init.com",&s6);
readfile(i,buf,s6);
i.close();
strncpy((char*)dir->dir_name,"INIT COM",11);
dir->dir_fst_clusHI = 0;
dir->dir_fst_clusLO = j;
dir->dir_file_size = s6;
for(int is=0;is<s6;is+=4096)
{
fat[j] = j+1;
j++;
buf += 4096;
}
fat[j-1] = EOC;
openfile(i,"shell.com",&s7);
readfile(i,buf,s7);
i.close();
strncpy((char*)dirs->dir_name,"SHELL COM",11);
dirs->dir_fst_clusHI = 0;
dirs->dir_fst_clusLO = j;
dirs->dir_file_size = s7;
printf("start of shell %d\n",j);
for(int ss=0;ss<s7;ss+=4096)
{
fat[j] = j+1;
j++;
}
fat[j-1] = EOC;
std::ofstream of("jazmyn",std::ios::trunc|std::ios::binary);
of.write(obuf,IMGSZ);
of.close();
delete obuf;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -