ReactOS 0.4.16-dev-329-g9223134
finstext2.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <string.h>
3#include "finstext2.h"
4#include "ext2.h" // #defines ext2_data
5#include <linux/hdreg.h>
6#include <linux/ext3_fs.h>
7#include <sys/ioctl.h>
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <fcntl.h>
11#include <unistd.h>
12
13#define MAJOR_VERSION 1
14#define MINOR_VERSION 0
15
18struct hd_geometry Ext2DriveGeometry;
19unsigned char Ext2ExistingBootCode[1024];
20
21char BlockDevice[260];
24
25int main(int argc, char *argv[])
26{
27 int Index;
28 char ch;
29
30 // Verify that we got enough command line parameters
31 if (argc < 2)
32 {
33 printf("finstext2 block_device [-v]\n\n");
34 printf("block_device Specifies the ext2/3 volume to install to (i.e. /dev/hda1)\n");
35 printf("-v Displays the version\n\n");
36 }
37
38 // Parse the command line parameters
39 for (Index=1; Index<argc; Index++)
40 {
41 if (strcmp(argv[Index], "-v") == 0)
42 {
43 printf("FreeLoader Ext2/3 Installer v%d.%d\n", MAJOR_VERSION, MINOR_VERSION);
44 }
45 else
46 {
49 }
50 }
51
53 {
55
57 {
58 printf("Couldn't open block device %s\n", BlockDevice);
59 return 1;
60 }
61
63 {
65 printf("Couldn't read existing boot code from %s\n", BlockDevice);
66 return 1;
67 }
68
69 for (Index=0; Index<1024; Index++)
70 {
71 if (Ext2ExistingBootCode[Index] != 0x00)
72 {
73 printf("This EXT2/3 volume has existing boot code.\n");
74 printf("Do you want to overwrite it? [y/n] ");
75 scanf("%c", &ch);
76
77 if (ch == 'n' || ch == 'N')
78 {
80 printf("Cancelled.\n");
81 return 0;
82 }
83
84 break;
85 }
86 }
87
88 if (read(BlockDeviceFileDescriptor, &Ext2SuperBlock, (size_t)1024) != (ssize_t)1024)
89 {
91 printf("Couldn't read super block from %s\n", BlockDevice);
92 return 1;
93 }
94
95 if (Ext2SuperBlock.s_magic != EXT3_SUPER_MAGIC)
96 {
98 printf("Block device %s is not a EXT2/3 volume or has an invalid super block.\n", BlockDevice);
99 return 1;
100 }
101
102 if (ioctl(BlockDeviceFileDescriptor, HDIO_GETGEO, &Ext2DriveGeometry) != 0)
103 {
105 printf("Couldn't get drive geometry from block device %s\n", BlockDevice);
106 return 1;
107 }
108
109 printf("Heads: %d\n", Ext2DriveGeometry.heads);
110 printf("Sectors: %d\n", Ext2DriveGeometry.sectors);
111 printf("Cylinders: %d\n", Ext2DriveGeometry.cylinders);
112 printf("Start: %d\n", Ext2DriveGeometry.start);
113
114 Ext2BootCode->BootDrive = 0xff;
116 //Ext2BootCode->SectorsPerTrack = Ext2DriveGeometry.sectors;
117 //Ext2BootCode->NumberOfHeads = Ext2DriveGeometry.heads;
119 Ext2BootCode->Ext2BlockSizeInBytes = 1024 << Ext2SuperBlock.s_log_block_size;
123 Ext2BootCode->Ext2FirstDataBlock = Ext2SuperBlock.s_first_data_block;
124 Ext2BootCode->Ext2InodesPerGroup = Ext2SuperBlock.s_inodes_per_group;
126
128 {
130 printf("Couldn't write boot code on %s\n", BlockDevice);
131 return 1;
132 }
133
134 if (write(BlockDeviceFileDescriptor, Ext2BootCode, (size_t)1024) != (ssize_t)1024)
135 {
137 printf("Couldn't write boot code on %s\n", BlockDevice);
138 return 1;
139 }
140
142
143 printf("Boot code written successfully!\n");
144 }
145
146 return 0;
147}
static int argc
Definition: ServiceArgs.c:12
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
#define read
Definition: acwin.h:96
#define open
Definition: acwin.h:95
#define close
Definition: acwin.h:98
#define write
Definition: acwin.h:97
int main()
Definition: test.c:6
__kernel_off_t off_t
Definition: linux.h:201
int BlockDeviceFileDescriptor
Definition: finstext2.c:23
unsigned char Ext2ExistingBootCode[1024]
Definition: finstext2.c:19
#define MINOR_VERSION
Definition: finstext2.c:14
int BlockDeviceSpecified
Definition: finstext2.c:22
struct hd_geometry Ext2DriveGeometry
Definition: finstext2.c:18
PEXT2_BOOTCODE Ext2BootCode
Definition: finstext2.c:16
#define MAJOR_VERSION
Definition: finstext2.c:13
struct ext3_super_block Ext2SuperBlock
Definition: finstext2.c:17
char BlockDevice[260]
Definition: finstext2.c:21
struct PACKED * PEXT2_BOOTCODE
#define printf
Definition: freeldr.h:97
#define O_RDWR
Definition: fcntl.h:36
_Check_return_ _CRTIMP int __cdecl scanf(_In_z_ _Scanf_format_string_ const char *_Format,...)
#define SEEK_SET
Definition: jmemansi.c:26
#define lseek
Definition: syshdrs.h:47
#define argv
Definition: mplay32.c:18
int ssize_t
Definition: rosdhcp.h:48
unsigned long Ext2InodesPerBlock
Definition: finstext2.h:22
unsigned long Ext2InodesPerGroup
Definition: finstext2.h:21
unsigned char BootDrive
Definition: finstext2.h:8
unsigned long Ext2PointersPerBlock
Definition: finstext2.h:18
unsigned long Ext2FirstDataBlock
Definition: finstext2.h:20
unsigned char BootPartition
Definition: finstext2.h:26
unsigned long Ext2BlockSizeInBytes
Definition: finstext2.h:17
unsigned long Ext2BlockSize
Definition: finstext2.h:16
unsigned long Ext2VolumeStartSector
Definition: finstext2.h:15
unsigned long Ext2GroupDescPerBlock
Definition: finstext2.h:19
_In_ WDFCOLLECTION _In_ ULONG Index
#define ioctl
Definition: wintirpc.h:60