Guest User

Untitled

a guest
Mar 19th, 2012
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. typedef unsigned char u8;
  7. typedef unsigned short u16;
  8. typedef unsigned int u32;
  9.  
  10. #define END_OF_CHAIN 0x0FFFFFFF
  11.  
  12. #pragma pack(1)
  13. struct primary_partition
  14. {
  15.     u8 Status;
  16.     u8 CHSAddressStart[3];
  17.     u8 PartitionType;
  18.     u8 CHSAddressEnd[3];
  19.     u32 LBAAddressStart;
  20.     u32 SecotrCount;
  21. };
  22.  
  23. #pragma pack(1)
  24. struct fat32_header
  25. {
  26.     char Jump[3];
  27.     char OEMName[8];
  28.     u16 BytesPerSector;
  29.     u8 SectorsPerCluster;
  30.     u16 ReservedSectors;
  31.     u8 NumberOfFats;
  32.     u16 MaxRootEntries;
  33.     u16 TotalSectors;
  34.     u8 MediaDescriptor;
  35.     u16 SectorsPerFat;
  36.     u16 SectorsPerTrack;
  37.     u16 NumberOfHeads;
  38.     u32 HiddenSectors;
  39.     u32 TotalSectorsBig;
  40.     u32 SectorsPerFatBig;
  41.     u16 ExtendedFlags;
  42.     u16 FSVersion;
  43.     u32 RootDirStartCluster;
  44.     u16 FSInfoSector;
  45.     u16 BackupBootSector;
  46.     u8 Reserved1[12];
  47.     u8 BootDrive;
  48.     u8 Reserved2;
  49.     u8 ExtendedSignature;
  50.     u32 SerialNumber;
  51.     char VolumeLabel[11];
  52.     char Filesystem[8];
  53.     char BootCode[420];
  54.     unsigned short BootSignature;
  55. };
  56.  
  57. #pragma pack(1)
  58. struct fat_83entry
  59. {
  60.     char Filename[8];
  61.     char Extension[3];
  62.     u8 Attributes;
  63.     u8 Reserved;
  64.     u8 CreationTimeTenths;
  65.     u16 CreationTime;
  66.     u16 CreationDate;
  67.     u16 LastAccessData;
  68.     u16 HiFirstCluster;
  69.     u16 LastModificationTime;
  70.     u16 LastModificationDate;
  71.     u16 LoFirstCluster;
  72.     u32 Filesize;
  73. };
  74.  
  75. #pragma pack(1)
  76. struct fat_lfnentry
  77. {
  78.     unsigned char Order;
  79.     wchar_t Chars1[5];
  80.     unsigned char Attribute;
  81.     unsigned char EntryType;
  82.     unsigned char Checksum;
  83.     wchar_t Chars2[6];
  84.     unsigned short Zero;
  85.     wchar_t Chars3[2];
  86. };
  87.  
  88. unsigned char *imgbuff;
  89. fat32_header *header;
  90. unsigned int *fattable;
  91. char fat32id[8] = { 'F', 'A', 'T', '3', '2', ' ', ' ', ' ' };
  92.  
  93.  
  94.  
  95. unsigned char* GetCluster(int cluster)
  96. {
  97.     int fds = (header->ReservedSectors + (header->NumberOfFats * header->SectorsPerFatBig)) * 512;
  98.     return (unsigned char*) ( ((unsigned int)imgbuff) + fds + ((cluster - 2) * header->SectorsPerCluster * header->BytesPerSector));
  99. }
  100.  
  101. int AddFile(char *path, wchar_t *lfnname, char *name)
  102. {
  103.     FILE *file = fopen(path, "rb");
  104.     if (!file)
  105.         return 1;
  106.  
  107.     fat_83entry *rootdir = (fat_83entry*)GetCluster(header->RootDirStartCluster);
  108.     int curdirentry = 0;
  109.     while (rootdir[curdirentry].Filename[0])
  110.         curdirentry++;
  111.  
  112.     int curcluster = 0;
  113.     while (fattable[curcluster])
  114.         curcluster++;
  115.  
  116.     if (lfnname)
  117.     {
  118.         int lfnlen = wcslen(lfnname);
  119.         int lfnentries = lfnlen / 13;
  120.         if (lfnlen % 13)
  121.             lfnentries++;
  122.  
  123.         int curchar = 0;
  124.         for (int i = 0; i < lfnentries; i++)
  125.         {
  126.             ((fat_lfnentry*)rootdir)[curdirentry].Attribute = 0xF;
  127.             ((fat_lfnentry*)rootdir)[curdirentry].Order = (lfnentries - i);
  128.             if (!i)
  129.                 ((fat_lfnentry*)rootdir)[curdirentry].Order += 0x40;
  130.  
  131.             int ltocpy = lfnlen < 5 ? lfnlen : 5;
  132.             memcpy(((fat_lfnentry*)rootdir)[curdirentry].Chars1, &lfnname[curchar], ltocpy * 2);
  133.             lfnlen -= ltocpy;
  134.             curchar += ltocpy;
  135.  
  136.             ltocpy = lfnlen < 6 ? lfnlen : 6;
  137.             memcpy(((fat_lfnentry*)rootdir)[curdirentry].Chars2, &lfnname[curchar], ltocpy * 2);
  138.             lfnlen -= ltocpy;
  139.             curchar += ltocpy;
  140.  
  141.             ltocpy = lfnlen < 2 ? lfnlen : 2;
  142.             memcpy(((fat_lfnentry*)rootdir)[curdirentry].Chars3, &lfnname[curchar], ltocpy * 2);
  143.             lfnlen -= ltocpy;
  144.             curchar += ltocpy;
  145.  
  146.             curdirentry++;
  147.         }
  148.     }
  149.  
  150.     memcpy(rootdir[curdirentry].Filename, name, 11);
  151.     rootdir[curdirentry].HiFirstCluster = (u16)(curcluster >> 16);
  152.     rootdir[curdirentry].LoFirstCluster = (u16)(curcluster & 0xFFFF);
  153.  
  154.     fseek(file, 0, SEEK_END);
  155.     size_t fsize = ftell(file);
  156.     fseek(file, 0, SEEK_SET);
  157.     unsigned char *fbuff = new unsigned char[fsize];
  158.     fread(fbuff, 1, fsize, file);
  159.     fclose(file);
  160.  
  161.     int clustersreq = fsize / 4096;
  162.     if (fsize % 4096)
  163.         clustersreq++;
  164.  
  165.     for (int i = 0; i < clustersreq; i++)
  166.     {
  167.         unsigned char *clusterptr = GetCluster(curcluster);
  168.         int writelen = fsize < 4096 ? fsize : 4096;
  169.         memcpy(clusterptr, &fbuff[i * 4096], writelen);
  170.         fsize -= writelen;
  171.  
  172.         if (i == (clustersreq - 1))
  173.             fattable[curcluster] = END_OF_CHAIN;
  174.         else
  175.         {
  176.             int oc = curcluster;
  177.             curcluster++;
  178.             while (fattable[curcluster])
  179.                 curcluster++;
  180.             fattable[oc] = curcluster;
  181.  
  182.         }
  183.     }
  184.  
  185.     return 0;
  186. }
  187.  
  188. int main(int argc, char **argv)
  189. {
  190.     imgbuff = new unsigned char[1048576 * 6];
  191.     memset(imgbuff, 0, 1048576 * 6);
  192.     header = (fat32_header*)imgbuff;
  193.  
  194.     header->BytesPerSector = 512;
  195.     header->SectorsPerCluster = 8;
  196.     header->ReservedSectors = 38;
  197.     header->NumberOfFats = 2;
  198.     header->MediaDescriptor = 248;
  199.     header->SectorsPerTrack = 32;
  200.     header->NumberOfHeads = 64;
  201.     header->TotalSectorsBig = 262144;
  202.     header->SectorsPerFatBig = 256;
  203.     header->RootDirStartCluster = 2;
  204.     header->FSInfoSector = 1;
  205.     header->BackupBootSector = 6;
  206.     header->BootSignature = 0xAA55;
  207.     header->Jump[0] = 0xEB;
  208.     header->Jump[1] = 0x58;
  209.     header->Jump[2] = 0x90;
  210.  
  211.     for (int i = 0; i < 8; i++)
  212.         header->Filesystem[i] = fat32id[i];
  213.  
  214.     FILE *mbr = fopen("D:\\Cygwin\\wnos\\output\\bin\\fat32mbr.bin", "rb");
  215.     if (!mbr)
  216.     {
  217.         printf("Could not open fat32mbr.bin\n");
  218.         delete imgbuff;
  219.         return 1;
  220.     }
  221.  
  222.     fat32_header *mbrbuff = (fat32_header*)new unsigned char[512];
  223.     fread(mbrbuff, 1, 512, mbr);
  224.     fclose(mbr);
  225.  
  226.     for (int i = 0; i < 420; i++)
  227.         header->BootCode[i] = mbrbuff->BootCode[i];
  228.  
  229.     delete mbrbuff;
  230.  
  231.     fattable = (unsigned int*)(((unsigned int)imgbuff) + 512 * header->ReservedSectors);
  232.     fattable[0] = END_OF_CHAIN;
  233.     fattable[1] = END_OF_CHAIN;
  234.     fattable[2] = END_OF_CHAIN;
  235.    
  236.     if (AddFile("D:\\Cygwin\\wnos\\output\\bin\\ezloader.bin", NULL, "EZLOADERBIN"))
  237.     {
  238.         printf("Could not open ezloader.bin\n");
  239.         delete imgbuff;
  240.         return 1;
  241.     }
  242.  
  243.     if (AddFile("D:\\Cygwin\\wnos\\output\\bin\\kernel.elf64", L"kernel.elf64", "KERNEL~1ELF"))
  244.     {
  245.         printf("Could not open kernel.elf64\n");
  246.         delete imgbuff;
  247.         return 1;
  248.     }
  249.  
  250.     FILE *out = fopen("D:\\Cygwin\\wnos\\output\\bin\\hddimg.img", "wb");
  251.     fwrite(imgbuff, 1, 1048576 * 6, out);
  252.     fclose(out);
  253.     delete imgbuff;
  254.     return 0;
  255. }
Advertisement
Add Comment
Please, Sign In to add comment