1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker
3*6a54128fSAndroid Build Coastguard Worker /usr/src/ext2ed/disk.c
4*6a54128fSAndroid Build Coastguard Worker
5*6a54128fSAndroid Build Coastguard Worker A part of the extended file system 2 disk editor.
6*6a54128fSAndroid Build Coastguard Worker
7*6a54128fSAndroid Build Coastguard Worker -------------------------------------------------
8*6a54128fSAndroid Build Coastguard Worker The filesystem's disk activity pass through here.
9*6a54128fSAndroid Build Coastguard Worker -------------------------------------------------
10*6a54128fSAndroid Build Coastguard Worker
11*6a54128fSAndroid Build Coastguard Worker This file is acting as a filter - Before we pass an actual read or write request to the operating system, we
12*6a54128fSAndroid Build Coastguard Worker double check the various permissions and possible errors here.
13*6a54128fSAndroid Build Coastguard Worker
14*6a54128fSAndroid Build Coastguard Worker The major update which needs to be done here is switching to the use of the llseek system call, so that we will
15*6a54128fSAndroid Build Coastguard Worker be able to support ext2 filesystems up to 4 TB. Currently, due to the standard fseek usage, we can't handle
16*6a54128fSAndroid Build Coastguard Worker filesystems bigger than 4 GB. The limit is actually 2 GB because I used long rather than unsigned long long at too
17*6a54128fSAndroid Build Coastguard Worker many places in the program. To conclude - This upgrade needs to be done carefully; There are many places to change.
18*6a54128fSAndroid Build Coastguard Worker
19*6a54128fSAndroid Build Coastguard Worker First written on: April 9 1995
20*6a54128fSAndroid Build Coastguard Worker
21*6a54128fSAndroid Build Coastguard Worker Copyright (C) 1995 Gadi Oxman
22*6a54128fSAndroid Build Coastguard Worker
23*6a54128fSAndroid Build Coastguard Worker */
24*6a54128fSAndroid Build Coastguard Worker
25*6a54128fSAndroid Build Coastguard Worker #include "config.h"
26*6a54128fSAndroid Build Coastguard Worker #include <stdio.h>
27*6a54128fSAndroid Build Coastguard Worker #include <stdlib.h>
28*6a54128fSAndroid Build Coastguard Worker #include <string.h>
29*6a54128fSAndroid Build Coastguard Worker #include <time.h>
30*6a54128fSAndroid Build Coastguard Worker
31*6a54128fSAndroid Build Coastguard Worker #include "ext2ed.h"
32*6a54128fSAndroid Build Coastguard Worker
33*6a54128fSAndroid Build Coastguard Worker int write_access;
34*6a54128fSAndroid Build Coastguard Worker
low_read(unsigned char * buffer,unsigned long length,unsigned long offset)35*6a54128fSAndroid Build Coastguard Worker int low_read (unsigned char *buffer,unsigned long length,unsigned long offset)
36*6a54128fSAndroid Build Coastguard Worker
37*6a54128fSAndroid Build Coastguard Worker /*
38*6a54128fSAndroid Build Coastguard Worker
39*6a54128fSAndroid Build Coastguard Worker This function is used when we need to read something from the filesystem.
40*6a54128fSAndroid Build Coastguard Worker
41*6a54128fSAndroid Build Coastguard Worker */
42*6a54128fSAndroid Build Coastguard Worker
43*6a54128fSAndroid Build Coastguard Worker {
44*6a54128fSAndroid Build Coastguard Worker
45*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
46*6a54128fSAndroid Build Coastguard Worker
47*6a54128fSAndroid Build Coastguard Worker char temp [80];
48*6a54128fSAndroid Build Coastguard Worker
49*6a54128fSAndroid Build Coastguard Worker if (device_handle==NULL) { /* Check that a device is indeed open */
50*6a54128fSAndroid Build Coastguard Worker internal_error ("No device opened yet read requested","disk","low_read");
51*6a54128fSAndroid Build Coastguard Worker return (0);
52*6a54128fSAndroid Build Coastguard Worker }
53*6a54128fSAndroid Build Coastguard Worker if (offset > file_system_info.file_system_size) { /* Check that the offset is within limits */
54*6a54128fSAndroid Build Coastguard Worker sprintf (temp,"Seek offset %ld is out of range",offset);
55*6a54128fSAndroid Build Coastguard Worker internal_error (temp,"disk","low_read");
56*6a54128fSAndroid Build Coastguard Worker return (0);
57*6a54128fSAndroid Build Coastguard Worker }
58*6a54128fSAndroid Build Coastguard Worker
59*6a54128fSAndroid Build Coastguard Worker #endif
60*6a54128fSAndroid Build Coastguard Worker
61*6a54128fSAndroid Build Coastguard Worker if ( (fseek (device_handle,offset,SEEK_SET))==-1) { /* Seek to the required offset */
62*6a54128fSAndroid Build Coastguard Worker wprintw (command_win,"Error - Failed to seek to offset %ld in device %s\n",offset,device_name);
63*6a54128fSAndroid Build Coastguard Worker refresh_command_win ();
64*6a54128fSAndroid Build Coastguard Worker return (0);
65*6a54128fSAndroid Build Coastguard Worker };
66*6a54128fSAndroid Build Coastguard Worker
67*6a54128fSAndroid Build Coastguard Worker if ( (fread (buffer,1,length,device_handle))==-1) { /* And do the actual reading */
68*6a54128fSAndroid Build Coastguard Worker wprintw (command_win,"Error - Failed to read from offset %ld in device %s\n",offset,device_name);
69*6a54128fSAndroid Build Coastguard Worker refresh_command_win ();return (0);
70*6a54128fSAndroid Build Coastguard Worker };
71*6a54128fSAndroid Build Coastguard Worker
72*6a54128fSAndroid Build Coastguard Worker return (1);
73*6a54128fSAndroid Build Coastguard Worker }
74*6a54128fSAndroid Build Coastguard Worker
low_write(unsigned char * buffer,unsigned long length,unsigned long offset)75*6a54128fSAndroid Build Coastguard Worker int low_write (unsigned char *buffer,unsigned long length,unsigned long offset)
76*6a54128fSAndroid Build Coastguard Worker
77*6a54128fSAndroid Build Coastguard Worker /*
78*6a54128fSAndroid Build Coastguard Worker
79*6a54128fSAndroid Build Coastguard Worker This is used to change something in the filesystem.
80*6a54128fSAndroid Build Coastguard Worker write_access is checked to see if we are allowed to do the actual writing.
81*6a54128fSAndroid Build Coastguard Worker As a double safety measure, AllowChanges is rechecked here.
82*6a54128fSAndroid Build Coastguard Worker If logging is enabled, we log the change before writing it to the device.
83*6a54128fSAndroid Build Coastguard Worker
84*6a54128fSAndroid Build Coastguard Worker */
85*6a54128fSAndroid Build Coastguard Worker {
86*6a54128fSAndroid Build Coastguard Worker char temp [80];
87*6a54128fSAndroid Build Coastguard Worker
88*6a54128fSAndroid Build Coastguard Worker if (!write_access) {
89*6a54128fSAndroid Build Coastguard Worker wprintw (command_win,"Error - Write access not available (use enablewrite)\n");
90*6a54128fSAndroid Build Coastguard Worker return (0);
91*6a54128fSAndroid Build Coastguard Worker }
92*6a54128fSAndroid Build Coastguard Worker
93*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
94*6a54128fSAndroid Build Coastguard Worker
95*6a54128fSAndroid Build Coastguard Worker if (!AllowChanges) {
96*6a54128fSAndroid Build Coastguard Worker internal_error ("AllowChanges=0 yet enablewrite succeeded","disk","low_write");
97*6a54128fSAndroid Build Coastguard Worker return (0);
98*6a54128fSAndroid Build Coastguard Worker }
99*6a54128fSAndroid Build Coastguard Worker
100*6a54128fSAndroid Build Coastguard Worker if (device_handle==NULL) {
101*6a54128fSAndroid Build Coastguard Worker internal_error ("No device opened yet read requested","disk","low_write");
102*6a54128fSAndroid Build Coastguard Worker return (0);
103*6a54128fSAndroid Build Coastguard Worker }
104*6a54128fSAndroid Build Coastguard Worker
105*6a54128fSAndroid Build Coastguard Worker if (offset > file_system_info.file_system_size) {
106*6a54128fSAndroid Build Coastguard Worker sprintf (temp,"Seek offset %ld is out of range",offset);
107*6a54128fSAndroid Build Coastguard Worker internal_error (temp,"disk","low_write");
108*6a54128fSAndroid Build Coastguard Worker return (0);
109*6a54128fSAndroid Build Coastguard Worker }
110*6a54128fSAndroid Build Coastguard Worker
111*6a54128fSAndroid Build Coastguard Worker #endif
112*6a54128fSAndroid Build Coastguard Worker
113*6a54128fSAndroid Build Coastguard Worker if (LogChanges)
114*6a54128fSAndroid Build Coastguard Worker if (!log_changes (buffer,length,offset))
115*6a54128fSAndroid Build Coastguard Worker return (0);
116*6a54128fSAndroid Build Coastguard Worker
117*6a54128fSAndroid Build Coastguard Worker if ( (fseek (device_handle,offset,SEEK_SET))==-1) {
118*6a54128fSAndroid Build Coastguard Worker wprintw (command_win,"Error - Failed to seek to offset %ld in device %s\n",offset,device_name);
119*6a54128fSAndroid Build Coastguard Worker refresh_command_win ();return (0);
120*6a54128fSAndroid Build Coastguard Worker };
121*6a54128fSAndroid Build Coastguard Worker
122*6a54128fSAndroid Build Coastguard Worker
123*6a54128fSAndroid Build Coastguard Worker if ( (fwrite (buffer,1,length,device_handle))==-1) {
124*6a54128fSAndroid Build Coastguard Worker wprintw (command_win,"Error - Failed to write to offset %ld in device %s\n",offset,device_name);
125*6a54128fSAndroid Build Coastguard Worker refresh_command_win ();return (0);
126*6a54128fSAndroid Build Coastguard Worker };
127*6a54128fSAndroid Build Coastguard Worker
128*6a54128fSAndroid Build Coastguard Worker wprintw (command_win,"Data written");refresh_command_win ();
129*6a54128fSAndroid Build Coastguard Worker return (1);
130*6a54128fSAndroid Build Coastguard Worker }
131*6a54128fSAndroid Build Coastguard Worker
log_changes(unsigned char * buffer,unsigned long length,unsigned long offset)132*6a54128fSAndroid Build Coastguard Worker int log_changes (unsigned char *buffer,unsigned long length,unsigned long offset)
133*6a54128fSAndroid Build Coastguard Worker
134*6a54128fSAndroid Build Coastguard Worker /*
135*6a54128fSAndroid Build Coastguard Worker
136*6a54128fSAndroid Build Coastguard Worker Log the change in a primitive form - An hex dump of the data before the change and after the change.
137*6a54128fSAndroid Build Coastguard Worker The hex bytes are converted to text, so that they will be readable with a standard text editor.
138*6a54128fSAndroid Build Coastguard Worker
139*6a54128fSAndroid Build Coastguard Worker */
140*6a54128fSAndroid Build Coastguard Worker
141*6a54128fSAndroid Build Coastguard Worker {
142*6a54128fSAndroid Build Coastguard Worker unsigned char *original;
143*6a54128fSAndroid Build Coastguard Worker
144*6a54128fSAndroid Build Coastguard Worker int i;
145*6a54128fSAndroid Build Coastguard Worker time_t current_time;
146*6a54128fSAndroid Build Coastguard Worker FILE *fp;
147*6a54128fSAndroid Build Coastguard Worker
148*6a54128fSAndroid Build Coastguard Worker if ((fp=fopen (LogFile,"a+"))==NULL) {
149*6a54128fSAndroid Build Coastguard Worker wprintw (command_win,"Error - Unable to open log file %s\n",LogFile);
150*6a54128fSAndroid Build Coastguard Worker refresh_command_win ();return (0);
151*6a54128fSAndroid Build Coastguard Worker };
152*6a54128fSAndroid Build Coastguard Worker
153*6a54128fSAndroid Build Coastguard Worker current_time=time (NULL);
154*6a54128fSAndroid Build Coastguard Worker
155*6a54128fSAndroid Build Coastguard Worker fprintf (fp,"\n----- EXT2ED log begin -----\n\n");
156*6a54128fSAndroid Build Coastguard Worker fprintf (fp,"Time: %s\nDevice: %s\n",ctime ((time_t *) ¤t_time),device_name);
157*6a54128fSAndroid Build Coastguard Worker fprintf (fp,"Offset: %lu\nLength: %lu\n",offset,length);
158*6a54128fSAndroid Build Coastguard Worker
159*6a54128fSAndroid Build Coastguard Worker original=(unsigned char *) malloc (length*sizeof (unsigned char));
160*6a54128fSAndroid Build Coastguard Worker
161*6a54128fSAndroid Build Coastguard Worker if (original==NULL) {
162*6a54128fSAndroid Build Coastguard Worker wprintw (command_win,"Fatal error - Can\'t allocate %lu bytes!");
163*6a54128fSAndroid Build Coastguard Worker refresh_command_win ();fclose (fp);return (0);
164*6a54128fSAndroid Build Coastguard Worker }
165*6a54128fSAndroid Build Coastguard Worker
166*6a54128fSAndroid Build Coastguard Worker if (!low_read (original,length,offset)) {
167*6a54128fSAndroid Build Coastguard Worker fclose (fp);return (0);
168*6a54128fSAndroid Build Coastguard Worker }
169*6a54128fSAndroid Build Coastguard Worker
170*6a54128fSAndroid Build Coastguard Worker fprintf (fp,"\nOriginal data:\n\n");
171*6a54128fSAndroid Build Coastguard Worker
172*6a54128fSAndroid Build Coastguard Worker for (i=0;i<length;i++) {
173*6a54128fSAndroid Build Coastguard Worker if (i%16==0 && i!=0) fprintf (fp,"\n");
174*6a54128fSAndroid Build Coastguard Worker fprintf (fp,"%02x ",original [i]);
175*6a54128fSAndroid Build Coastguard Worker }
176*6a54128fSAndroid Build Coastguard Worker
177*6a54128fSAndroid Build Coastguard Worker fprintf (fp,"\n\nNew data:\n\n");
178*6a54128fSAndroid Build Coastguard Worker
179*6a54128fSAndroid Build Coastguard Worker for (i=0;i<length;i++) {
180*6a54128fSAndroid Build Coastguard Worker if (i%16==0 && i!=0) fprintf (fp,"\n");
181*6a54128fSAndroid Build Coastguard Worker fprintf (fp,"%02x ",buffer [i]);
182*6a54128fSAndroid Build Coastguard Worker }
183*6a54128fSAndroid Build Coastguard Worker
184*6a54128fSAndroid Build Coastguard Worker fprintf (fp,"\n----- EXT2ED log end -----\n");
185*6a54128fSAndroid Build Coastguard Worker
186*6a54128fSAndroid Build Coastguard Worker fclose (fp);
187*6a54128fSAndroid Build Coastguard Worker return (1);
188*6a54128fSAndroid Build Coastguard Worker }
189*6a54128fSAndroid Build Coastguard Worker
load_type_data(void)190*6a54128fSAndroid Build Coastguard Worker int load_type_data (void)
191*6a54128fSAndroid Build Coastguard Worker
192*6a54128fSAndroid Build Coastguard Worker /*
193*6a54128fSAndroid Build Coastguard Worker
194*6a54128fSAndroid Build Coastguard Worker Just read from the current position into type data.
195*6a54128fSAndroid Build Coastguard Worker
196*6a54128fSAndroid Build Coastguard Worker */
197*6a54128fSAndroid Build Coastguard Worker
198*6a54128fSAndroid Build Coastguard Worker {
199*6a54128fSAndroid Build Coastguard Worker if (device_handle==NULL) {
200*6a54128fSAndroid Build Coastguard Worker printf ("Error - No device opened\n");
201*6a54128fSAndroid Build Coastguard Worker return (0);
202*6a54128fSAndroid Build Coastguard Worker }
203*6a54128fSAndroid Build Coastguard Worker
204*6a54128fSAndroid Build Coastguard Worker if (device_offset==-1) {
205*6a54128fSAndroid Build Coastguard Worker printf ("Error - No offset set\n");
206*6a54128fSAndroid Build Coastguard Worker return (0);
207*6a54128fSAndroid Build Coastguard Worker }
208*6a54128fSAndroid Build Coastguard Worker
209*6a54128fSAndroid Build Coastguard Worker if (low_read (type_data.u.buffer,EXT2_MAX_BLOCK_SIZE,device_offset)==0)
210*6a54128fSAndroid Build Coastguard Worker return (0);
211*6a54128fSAndroid Build Coastguard Worker
212*6a54128fSAndroid Build Coastguard Worker if (current_type!=NULL)
213*6a54128fSAndroid Build Coastguard Worker if (strcmp (current_type->name,"ext2_dir_entry")==0)
214*6a54128fSAndroid Build Coastguard Worker current_type->length=type_data.u.t_ext2_dir_entry.rec_len;
215*6a54128fSAndroid Build Coastguard Worker
216*6a54128fSAndroid Build Coastguard Worker return (1);
217*6a54128fSAndroid Build Coastguard Worker }
218*6a54128fSAndroid Build Coastguard Worker
write_type_data(void)219*6a54128fSAndroid Build Coastguard Worker int write_type_data (void)
220*6a54128fSAndroid Build Coastguard Worker
221*6a54128fSAndroid Build Coastguard Worker {
222*6a54128fSAndroid Build Coastguard Worker if (device_handle==NULL) {
223*6a54128fSAndroid Build Coastguard Worker wprintw (command_win,"Error - No device opened\n");
224*6a54128fSAndroid Build Coastguard Worker refresh_command_win ();
225*6a54128fSAndroid Build Coastguard Worker return (0);
226*6a54128fSAndroid Build Coastguard Worker }
227*6a54128fSAndroid Build Coastguard Worker
228*6a54128fSAndroid Build Coastguard Worker if (device_offset==-1) {
229*6a54128fSAndroid Build Coastguard Worker wprintw (command_win,"Error - No offset set\n");
230*6a54128fSAndroid Build Coastguard Worker refresh_command_win ();
231*6a54128fSAndroid Build Coastguard Worker return (0);
232*6a54128fSAndroid Build Coastguard Worker }
233*6a54128fSAndroid Build Coastguard Worker
234*6a54128fSAndroid Build Coastguard Worker if (low_write (type_data.u.buffer,file_system_info.block_size,device_offset)==0)
235*6a54128fSAndroid Build Coastguard Worker return (0);
236*6a54128fSAndroid Build Coastguard Worker
237*6a54128fSAndroid Build Coastguard Worker return (1);
238*6a54128fSAndroid Build Coastguard Worker }
239*6a54128fSAndroid Build Coastguard Worker
240