-
Notifications
You must be signed in to change notification settings - Fork 16
/
cmdline_parser.c
200 lines (181 loc) · 5.53 KB
/
cmdline_parser.c
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
#include "ofgwrite.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
// search device table for specific partition names
int search_via_part_names(char* device_table)
{
int partition_number = 1;
char device_name[100];
char cmp_kernel_name[50];
char cmp_rootfs_name[50];
char* pos;
// Search for rootfs and kernel partitions. Both have to be on the same device.
if (strstr(device_table, "(kernel)") != NULL && strstr(device_table, "(dreambox-rootfs)") != NULL)
{
strcpy(cmp_kernel_name, "(kernel)");
strcpy(cmp_rootfs_name, "(dreambox-rootfs)");
}
else if (strstr(device_table, "(kernel)") != NULL && strstr(device_table, "(rootfs)") != NULL)
{
strcpy(cmp_kernel_name, "(kernel)");
strcpy(cmp_rootfs_name, "(rootfs)");
}
else if (strstr(device_table, "(ekernel)") != NULL && strstr(device_table, "(rootfs)") != NULL)
{
strcpy(cmp_kernel_name, "(ekernel)");
strcpy(cmp_rootfs_name, "(rootfs)");
}
else if (strstr(device_table, "(exkernel)") != NULL && strstr(device_table, "(exrootfs)") != NULL)
{
strcpy(cmp_kernel_name, "(exkernel)");
strcpy(cmp_rootfs_name, "(exrootfs)");
}
else if (strstr(device_table, "(boot)") != NULL && strstr(device_table, "(root)") != NULL)
{
strcpy(cmp_kernel_name, "(boot)");
strcpy(cmp_rootfs_name, "(root)");
}
else if (strstr(device_table, "(linuxkernel)") != NULL && strstr(device_table, "(linuxrootfs)") != NULL)
{
strcpy(cmp_kernel_name, "(linuxkernel)");
strcpy(cmp_rootfs_name, "(linuxrootfs)");
}
else if ( access( "/dev/block/by-name/flag", F_OK) != -1 && current_rootfs_sub_dir[0] != '\0')
{
sprintf(cmp_kernel_name, "(linuxkernel%d)", multiboot_partition);
strcpy(cmp_rootfs_name, "(rootfs)");
sprintf(rootfs_sub_dir, "linuxrootfs%d", multiboot_partition);
}
else if (current_rootfs_sub_dir[0] != '\0') // box with rootSubDir feature
{
sprintf(cmp_kernel_name, "(linuxkernel%d)", multiboot_partition);
strcpy(cmp_rootfs_name, "(userdata)");
sprintf(rootfs_sub_dir, "linuxrootfs%d", multiboot_partition);
}
else
return 0;
// read device name
if ((pos = strstr(device_table, ":")) == NULL)
{
my_printf("Error: No device name in /proc/cmdline blkdevparts: %s\n", device_table);
return -1;
}
strncpy(device_name, device_table, pos - device_table);
device_name[pos - device_table] = '\0';
device_table = pos + 1;
while (device_table)
{
if ((pos = strstr(device_table, ",")) != NULL)
*pos = '\0';
if (strstr(device_table, cmp_kernel_name) != NULL)
{
found_kernel_device = 1;
kernel_flash_mode = TARBZ2;
sprintf(kernel_device, "/dev/%sp%d", device_name, partition_number);
}
else if (strstr(device_table, cmp_rootfs_name) != NULL)
{
found_rootfs_device = 1;
rootfs_flash_mode = TARBZ2;
sprintf(rootfs_device, "/dev/%sp%d", device_name, partition_number);
}
if (pos)
device_table = ++pos;
else
break;
partition_number++;
}
if (found_kernel_device)
my_printf("Found cmdLine kernel device: %s\n", kernel_device);
if (found_rootfs_device)
my_printf("Found cmdLine rootfs device: %s\n", rootfs_device);
if (found_kernel_device && found_rootfs_device)
return 1;
else
{
my_printf("No partitions found in /proc/cmdline\n");
return -1;
}
}
// check whether devices point to valid partitions
int search_current_used_partitions(char* device_table)
{
int partition_number = 1;
char device_name[100];
char part_name[100];
char* pos;
// read device name
if ((pos = strstr(device_table, ":")) == NULL && device_table != pos)
{
my_printf("Error: No device name in /proc/cmdline blkdevparts: %s\n", device_table);
return -1;
}
pos[0] = '\0';
sprintf(device_name, "/dev/%sp", device_table);
device_table = pos + 1;
if (strstr(current_rootfs_device, device_name) == NULL || strstr(current_kernel_device, device_name) == NULL)
return -1; // rootfs or kernel are located on other device
while (device_table)
{
if ((pos = strstr(device_table, ",")) != NULL)
*pos = '\0';
sprintf(part_name, "%s%d", device_name, partition_number);
if (strstr(part_name, current_kernel_device) != NULL && strstr(device_table, "(linuxkernel") != NULL && current_kernel_device[0] != '\0')
{
found_kernel_device = 1;
kernel_flash_mode = TARBZ2;
strcpy(kernel_device, current_kernel_device);
}
else if (strstr(part_name, current_rootfs_device) != NULL && strstr(device_table, "(userdata)") != NULL && current_rootfs_device[0] != '\0')
{
found_rootfs_device = 1;
rootfs_flash_mode = TARBZ2;
strcpy(rootfs_device, current_rootfs_device);
}
if (pos)
device_table = ++pos;
else
break;
partition_number++;
}
if (found_kernel_device)
my_printf("Using cmdLine kernel device: %s\n", kernel_device);
if (found_rootfs_device)
my_printf("Using cmdLine rootfs device: %s\n", rootfs_device);
if (found_kernel_device && found_rootfs_device)
{
strcpy(rootfs_sub_dir, current_rootfs_sub_dir);
return 1;
}
else
{
my_printf("Error: Wrong or missing kernel/root in /proc/cmdline\n");
return -1;
}
}
void parse_cmdline_partition_table(char* cmdline)
{
int ret;
char* next_device;
char* end;
// cut off rest of cmdline
if ((end = strstr(cmdline, " ")) != NULL)
*end = '\0';
while (cmdline)
{
if ((next_device = strstr(cmdline, ";")) != NULL)
*next_device = '\0';
if (current_rootfs_sub_dir[0] != '\0' && multiboot_partition == -1)
// flash current running image -> check whether devices point to valid partitions
ret = search_current_used_partitions(cmdline);
else
ret = search_via_part_names(cmdline);
if (ret != 0)
break;
if (next_device)
cmdline = ++next_device;
else
break;
}
}