Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenfinstext2.c
Go to the documentation of this file.
00001 #include <stdio.h> 00002 #include <string.h> 00003 #include "finstext2.h" 00004 #include "ext2.h" // #defines ext2_data 00005 #include <linux/hdreg.h> 00006 #include <linux/ext3_fs.h> 00007 #include <sys/ioctl.h> 00008 #include <sys/types.h> 00009 #include <sys/stat.h> 00010 #include <fcntl.h> 00011 #include <unistd.h> 00012 00013 #define MAJOR_VERSION 1 00014 #define MINOR_VERSION 0 00015 00016 PEXT2_BOOTCODE Ext2BootCode = (PEXT2_BOOTCODE)ext2_data; 00017 struct ext3_super_block Ext2SuperBlock; 00018 struct hd_geometry Ext2DriveGeometry; 00019 unsigned char Ext2ExistingBootCode[1024]; 00020 00021 char BlockDevice[260]; 00022 int BlockDeviceSpecified = 0; 00023 int BlockDeviceFileDescriptor; 00024 00025 int main(int argc, char *argv[]) 00026 { 00027 int Index; 00028 char ch; 00029 00030 // Verify that we got enough command line parameters 00031 if (argc < 2) 00032 { 00033 printf("finstext2 block_device [-v]\n\n"); 00034 printf("block_device Specifies the ext2/3 volume to install to (i.e. /dev/hda1)\n"); 00035 printf("-v Displays the version\n\n"); 00036 } 00037 00038 // Parse the command line parameters 00039 for (Index=1; Index<argc; Index++) 00040 { 00041 if (strcmp(argv[Index], "-v") == 0) 00042 { 00043 printf("FreeLoader Ext2/3 Installer v%d.%d\n", MAJOR_VERSION, MINOR_VERSION); 00044 } 00045 else 00046 { 00047 strcpy(BlockDevice, argv[Index]); 00048 BlockDeviceSpecified = 1; 00049 } 00050 } 00051 00052 if (BlockDeviceSpecified) 00053 { 00054 BlockDeviceFileDescriptor = open(BlockDevice, O_RDWR|O_SYNC); 00055 00056 if (BlockDeviceFileDescriptor == -1) 00057 { 00058 printf("Couldn't open block device %s\n", BlockDevice); 00059 return 1; 00060 } 00061 00062 if (read(BlockDeviceFileDescriptor, Ext2ExistingBootCode, (size_t)1024) != (ssize_t)1024) 00063 { 00064 close(BlockDeviceFileDescriptor); 00065 printf("Couldn't read existing boot code from %s\n", BlockDevice); 00066 return 1; 00067 } 00068 00069 for (Index=0; Index<1024; Index++) 00070 { 00071 if (Ext2ExistingBootCode[Index] != 0x00) 00072 { 00073 printf("This EXT2/3 volume has existing boot code.\n"); 00074 printf("Do you want to overwrite it? [y/n] "); 00075 scanf("%c", &ch); 00076 00077 if (ch == 'n' || ch == 'N') 00078 { 00079 close(BlockDeviceFileDescriptor); 00080 printf("Cancelled.\n"); 00081 return 0; 00082 } 00083 00084 break; 00085 } 00086 } 00087 00088 if (read(BlockDeviceFileDescriptor, &Ext2SuperBlock, (size_t)1024) != (ssize_t)1024) 00089 { 00090 close(BlockDeviceFileDescriptor); 00091 printf("Couldn't read super block from %s\n", BlockDevice); 00092 return 1; 00093 } 00094 00095 if (Ext2SuperBlock.s_magic != EXT3_SUPER_MAGIC) 00096 { 00097 close(BlockDeviceFileDescriptor); 00098 printf("Block device %s is not a EXT2/3 volume or has an invalid super block.\n", BlockDevice); 00099 return 1; 00100 } 00101 00102 if (ioctl(BlockDeviceFileDescriptor, HDIO_GETGEO, &Ext2DriveGeometry) != 0) 00103 { 00104 close(BlockDeviceFileDescriptor); 00105 printf("Couldn't get drive geometry from block device %s\n", BlockDevice); 00106 return 1; 00107 } 00108 00109 printf("Heads: %d\n", Ext2DriveGeometry.heads); 00110 printf("Sectors: %d\n", Ext2DriveGeometry.sectors); 00111 printf("Cylinders: %d\n", Ext2DriveGeometry.cylinders); 00112 printf("Start: %d\n", Ext2DriveGeometry.start); 00113 00114 Ext2BootCode->BootDrive = 0xff; 00115 Ext2BootCode->BootPartition = 0x00; 00116 //Ext2BootCode->SectorsPerTrack = Ext2DriveGeometry.sectors; 00117 //Ext2BootCode->NumberOfHeads = Ext2DriveGeometry.heads; 00118 Ext2BootCode->Ext2VolumeStartSector = Ext2DriveGeometry.start; 00119 Ext2BootCode->Ext2BlockSizeInBytes = 1024 << Ext2SuperBlock.s_log_block_size; 00120 Ext2BootCode->Ext2BlockSize = Ext2BootCode->Ext2BlockSizeInBytes / 512; 00121 Ext2BootCode->Ext2PointersPerBlock = Ext2BootCode->Ext2BlockSizeInBytes / 4; 00122 Ext2BootCode->Ext2GroupDescPerBlock = Ext2BootCode->Ext2BlockSizeInBytes / 32; 00123 Ext2BootCode->Ext2FirstDataBlock = Ext2SuperBlock.s_first_data_block; 00124 Ext2BootCode->Ext2InodesPerGroup = Ext2SuperBlock.s_inodes_per_group; 00125 Ext2BootCode->Ext2InodesPerBlock = Ext2BootCode->Ext2BlockSizeInBytes / 128; 00126 00127 if (lseek(BlockDeviceFileDescriptor, (off_t)0, SEEK_SET) == (off_t)-1) 00128 { 00129 close(BlockDeviceFileDescriptor); 00130 printf("Couldn't write boot code on %s\n", BlockDevice); 00131 return 1; 00132 } 00133 00134 if (write(BlockDeviceFileDescriptor, Ext2BootCode, (size_t)1024) != (ssize_t)1024) 00135 { 00136 close(BlockDeviceFileDescriptor); 00137 printf("Couldn't write boot code on %s\n", BlockDevice); 00138 return 1; 00139 } 00140 00141 close(BlockDeviceFileDescriptor); 00142 00143 printf("Boot code written successfully!\n"); 00144 } 00145 00146 return 0; 00147 } Generated on Sat May 26 2012 04:18:07 for ReactOS by
1.7.6.1
|