1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
/*
* Guillaume Cottenceau (gc@mandrakesoft.com)
*
* Copyright 2000 MandrakeSoft
*
* This software may be freely redistributed under the terms of the GNU
* public license.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/*
* Portions from Erik Troan (ewt@redhat.com)
*
* Copyright 1996 Red Hat Software
*
*/
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include <zlib.h>
#include "stage1.h"
#include "log.h"
#include "mount.h"
#include "frontend.h"
#include "automatic.h"
#include "tools.h"
static struct param_elem * params;
void process_cmdline(void)
{
char buf[512];
int fd, size, i, p;
struct param_elem tmp_params[50];
log_message("opening /proc/cmdline... ");
if ((fd = open("/proc/cmdline", O_RDONLY, 0)) == -1)
fatal_error("could not open /proc/cmdline");
size = read(fd, buf, sizeof(buf));
buf[size-1] = 0;
close(fd);
log_message("\t%s", buf);
i = 0; p = 0;
while (buf[i] != '\0') {
char *name, *value = NULL;
int j = i;
while (buf[i] != ' ' && buf[i] != '=' && buf[i] != '\0')
i++;
if (i == j) {
i++;
continue;
}
name = memdup(&buf[j], i-j + 1);
name[i-j] = '\0';
if (buf[i] == '=') {
int k = i+1;
i++;
while (buf[i] != ' ' && buf[i] != '\0')
i++;
value = memdup(&buf[k], i-k + 1);
value[i-k] = '\0';
}
tmp_params[p].name = name;
tmp_params[p].value = value;
if (!strcmp(name, "expert")) set_param(MODE_EXPERT);
if (!strcmp(name, "rescue")) set_param(MODE_RESCUE);
if (!strcmp(name, "special_stage2")) set_param(MODE_SPECIAL_STAGE2);
if (!strcmp(name, "automatic")) {
set_param(MODE_AUTOMATIC);
grab_automatic_params(value);
}
p++;
if (buf[i] == '\0')
break;
i++;
}
if (IS_RESCUE) {
tmp_params[p].name = "special_stage2";
tmp_params[p].value = "rescue";
p++;
set_param(MODE_SPECIAL_STAGE2);
}
tmp_params[p++].name = NULL;
params = memdup(tmp_params, sizeof(struct param_elem) * p);
log_message("\tgot %d args", p-1);
}
int stage1_mode = 0;
int get_param(int i)
{
return (stage1_mode & i);
}
char * get_param_valued(char *param_name)
{
struct param_elem * ptr = params;
while (ptr->name) {
if (!strcmp(ptr->name, param_name))
return ptr->value;
ptr++;
}
return NULL;
}
void set_param(int i)
{
stage1_mode |= i;
}
void unset_param(int i)
{
stage1_mode &= ~i;
}
int total_memory(void)
{
int fd;
int i;
char buf[4096];
char * memtotal_tag = "MemTotal:";
int memtotal = 0;
fd = open("/proc/meminfo", O_RDONLY);
if (fd == -1)
fatal_error("could not open /proc/meminfo");
i = read(fd, buf, sizeof(buf));
if (i < 0)
fatal_error("could not read /proc/meminfo");
close(fd);
buf[i] = 0;
i = 0;
while (buf[i] != 0 && strncmp(&buf[i], memtotal_tag, strlen(memtotal_tag)))
i++;
while (buf[i] != 0 && buf[i] != '\n' && !isdigit(buf[i]))
i++;
if (buf[i] == 0 || buf[i] == '\n')
fatal_error("could not read MemTotal");
while (buf[i] != 0 && isdigit(buf[i])) {
memtotal = (memtotal * 10) + (buf[i] - '0');
i++;
}
log_message("%s %d kB", memtotal_tag, memtotal);
return memtotal;
}
int ramdisk_possible(void)
{
if (total_memory() > MEM_LIMIT_RAMDISK)
return 1;
else {
log_message("warning, ramdisk is not possible due to low mem!");
return 0;
}
}
enum return_type load_ramdisk(void)
{
char * img_name;
gzFile st2;
char * ramdisk = "/dev/ram3"; /* warning, verify that this file exists in the initrd (and actually is a ramdisk device file) */
int ram_fd, st2_fd;
char buffer[4096];
char * stg2_name = get_param_valued("special_stage2");
char * begin_img = "/tmp/image/Mandrake/base/";
char * end_img = "_stage2.gz";
int gz_errnum;
struct stat statr;
if (!stg2_name)
stg2_name = "mdkinst";
if (IS_RESCUE)
stg2_name = "rescue";
img_name = malloc(strlen(begin_img) + strlen(stg2_name) + strlen(end_img) + 1);
strcpy(img_name, begin_img);
strcat(img_name, stg2_name);
strcat(img_name, end_img);
log_message("trying to load %s as a ramdisk", img_name);
st2_fd = open(img_name, O_RDONLY); /* to be able to see the progression */
st2 = gzdopen(st2_fd, "r");
if (!st2) {
log_message("Opening compressed ramdisk: %s", gzerror(st2, &gz_errnum));
error_message("Could not open compressed ramdisk file.");
return RETURN_ERROR;
}
ram_fd = open(ramdisk, O_WRONLY);
if (ram_fd == -1) {
log_perror(ramdisk);
error_message("Could not open ramdisk device file.");
return RETURN_ERROR;
}
stat(img_name, &statr);
init_progression("Loading program into memory...", statr.st_size);
while (!gzeof(st2)) {
int actually = gzread(st2, buffer, sizeof(buffer));
if (actually != sizeof(buffer) && !gzeof(st2)) {
log_message("Reading compressed ramdisk: %s", gzerror(st2, &gz_errnum));
remove_wait_message();
return RETURN_ERROR;
}
if (write(ram_fd, buffer, actually) != actually) {
log_perror("Writing ramdisk");
remove_wait_message();
return RETURN_ERROR;
}
update_progression(lseek(st2_fd, 0L, SEEK_CUR));
}
end_progression();
gzclose(st2); /* opened by gzdopen, but also closes the associated fd */
close(ram_fd);
if (IS_RESCUE)
return RETURN_OK; /* fucksike, I lost several hours wondering why the kernel won't see the rescue if it is alreay mounted */
if (my_mount(ramdisk, "/tmp/stage2", "ext2"))
return RETURN_ERROR;
set_param(MODE_RAMDISK);
return RETURN_OK;
}
/* pixel's */
void * memdup(void *src, size_t size)
{
void * r;
r = malloc(size);
memcpy(r, src, size);
return r;
}
|