10
10
#include " initdrivethread.h"
11
11
#include " mbr.h"
12
12
#include " util.h"
13
+ #include " config.h"
13
14
#include < QProcess>
14
15
#include < QFile>
15
16
#include < QDir>
@@ -37,30 +38,22 @@ void InitDriveThread::run()
37
38
emit statusUpdate (tr (" Mounting FAT partition" ));
38
39
mountSystemPartition ();
39
40
40
- // if (sizeofBootFilesInKB() > (MAXIMUM_BOOTFILES_SIZE*1024))
41
- // {
42
- // emit error(tr("SD card contains extra files that do not belong to this distribution. Please copy them to another disk and delete them from card."));
43
- // return;
44
-
45
- // Try to resize existing partitions
46
- if (!method_resizePartitions ())
47
- {
48
- return ;
49
- }
50
- // }
51
- // else
52
- // {
53
- // // Reformat the drive
54
- // if (!method_reformatDrive())
55
- // {
56
- // return;
57
- // }
58
- // }
41
+ // Try to resize existing partitions
42
+ if (!method_resizePartitions ())
43
+ return ;
59
44
60
45
emit statusUpdate (tr (" Formatting settings partition" ));
61
46
if (!formatSettingsPartition ())
62
47
{
63
48
emit error (tr (" Error formatting settings partition" ));
49
+ return ;
50
+ }
51
+
52
+ emit statusUpdate (tr (" Mounting FAT partition" ));
53
+ if (!mountSystemPartition ())
54
+ {
55
+ emit error (tr (" Error mounting system partition." ));
56
+ return ;
64
57
}
65
58
66
59
dir.mkdir (" /mnt/os" );
@@ -128,53 +121,10 @@ void InitDriveThread::run()
128
121
emit completed ();
129
122
}
130
123
131
- bool InitDriveThread::method_reformatDrive ()
132
- {
133
- emit statusUpdate (tr (" Saving boot files to memory" ));
134
- if (!saveBootFiles () )
135
- {
136
- emit error (tr (" Error saving boot files to memory. SD card may be damaged." ));
137
- return false ;
138
- }
139
- if (!umountSystemPartition ())
140
- {
141
- emit error (tr (" Error unmounting system partition." ));
142
- return false ;
143
- }
144
-
145
- emit statusUpdate (tr (" Zeroing partition table" ));
146
- if (!zeroMbr ())
147
- {
148
- emit error (tr (" Error zero'ing MBR/GPT. SD card may be broken or advertising wrong capacity." ));
149
- return false ;
150
- }
151
-
152
- emit statusUpdate (tr (" Creating partitions" ));
153
-
154
- if (!partitionDrive ())
155
- {
156
- emit error (tr (" Error partitioning" ));
157
- return false ;
158
- }
159
-
160
- emit statusUpdate (tr (" Formatting boot partition (fat)" ));
161
- if (!formatBootPartition ())
162
- {
163
- emit error (tr (" Error formatting boot partition (fat)" ));
164
- return false ;
165
- }
166
-
167
- emit statusUpdate (tr (" Copying boot files to storage" ));
168
- mountSystemPartition ();
169
- restoreBootFiles ();
170
-
171
- return true ;
172
- }
173
-
174
124
bool InitDriveThread::method_resizePartitions ()
175
125
{
176
126
int newStartOfRescuePartition = getFileContents (" /sys/class/block/mmcblk0p1/start" ).trimmed ().toInt ();
177
- int newSizeOfRescuePartition = sizeofBootFilesInKB ()/1000 + 100 ;
127
+ int newSizeOfRescuePartition = sizeofBootFilesInKB ()* 1.024 /1000 + 100 ;
178
128
179
129
if (!umountSystemPartition ())
180
130
{
@@ -250,12 +200,17 @@ bool InitDriveThread::method_resizePartitions()
250
200
* only move it when it is not aligned on a MiB boundary already */
251
201
if (newStartOfRescuePartition < 2048 || newStartOfRescuePartition % 2048 != 0 )
252
202
{
253
- newStartOfRescuePartition = 8192 ; /* 4 MiB */
203
+ newStartOfRescuePartition = PARTITION_ALIGNMENT ; /* 4 MiB */
254
204
}
255
205
256
206
QString cmd = " /usr/sbin/parted --script /dev/mmcblk0 resize 1 " +QString::number (newStartOfRescuePartition)+" s " +QString::number (newSizeOfRescuePartition)+" M" ;
257
207
qDebug () << " Executing" << cmd;
258
208
QProcess p;
209
+ QProcessEnvironment env = QProcessEnvironment::systemEnvironment ();
210
+ /* Suppress parted's big fat warning about its file system manipulation code not being robust.
211
+ It distracts from any real error messages that may follow it. */
212
+ env.insert (" PARTED_SUPPRESS_FILE_SYSTEM_MANIPULATION_WARNING" , " 1" );
213
+ p.setProcessEnvironment (env);
259
214
p.setProcessChannelMode (p.MergedChannels );
260
215
p.start (cmd);
261
216
p.closeWriteChannel ();
@@ -271,33 +226,25 @@ bool InitDriveThread::method_resizePartitions()
271
226
272
227
emit statusUpdate (tr (" Creating extended partition" ));
273
228
274
- mbr_table extended_mbr;
275
229
QByteArray partitionTable;
276
230
int startOfOurPartition = getFileContents (" /sys/class/block/mmcblk0p1/start" ).trimmed ().toInt ();
277
231
int sizeOfOurPartition = getFileContents (" /sys/class/block/mmcblk0p1/size" ).trimmed ().toInt ();
278
232
int startOfExtended = startOfOurPartition+sizeOfOurPartition;
279
- // Align on 4 MiB boundary
280
- startOfExtended += 8192 -(startOfExtended % 8192 );
281
233
282
- int sizeOfDisk = getFileContents (" /sys/class/block/mmcblk0/size" ).trimmed ().toULongLong ();
283
- int sizeOfExtended = sizeOfDisk - startOfExtended - SETTINGS_PARTITION_SIZE ;
284
- int startOfSettings = startOfExtended+sizeOfExtended;
234
+ // Align start of settings partition on 4 MiB boundary
235
+ int startOfSettings = startOfExtended + PARTITION_GAP;
236
+ if (startOfSettings % PARTITION_ALIGNMENT != 0 )
237
+ startOfSettings += PARTITION_ALIGNMENT-(startOfSettings % PARTITION_ALIGNMENT);
285
238
239
+ // Primary partitions
286
240
partitionTable = QByteArray::number (startOfOurPartition)+" ," +QByteArray::number (sizeOfOurPartition)+" ,0E\n " ; /* FAT partition */
287
- partitionTable += QByteArray::number (startOfExtended)+" ," +QByteArray::number (sizeOfExtended)+" ,X\n " ; /* Extended partition with all remaining space */
288
- partitionTable += QByteArray::number (startOfSettings)+" ,,L\n " ; /* Settings partition */
241
+ partitionTable += QByteArray::number (startOfExtended)+" ,,X\n " ; /* Extended partition with all remaining space */
289
242
partitionTable += " 0,0\n " ;
243
+ partitionTable += " 0,0\n " ;
244
+ // Logical partitions
245
+ partitionTable += QByteArray::number (startOfSettings)+" ," +QByteArray::number (SETTINGS_PARTITION_SIZE)+" ,L\n " ; /* Settings partition */
290
246
qDebug () << " Writing partition table" << partitionTable;
291
247
292
- /* Write out empty extended partition table with signature */
293
- memset (&extended_mbr, 0 , sizeof extended_mbr);
294
- extended_mbr.signature [0 ] = 0x55 ;
295
- extended_mbr.signature [1 ] = 0xAA ;
296
- f.open (f.ReadWrite );
297
- f.seek (startOfExtended*512 );
298
- f.write ((char *) &extended_mbr, sizeof (extended_mbr));
299
- f.close ();
300
-
301
248
/* Let sfdisk write a proper partition table */
302
249
cmd = QString (" /sbin/sfdisk -uS /dev/mmcblk0" );
303
250
QProcess proc;
@@ -325,28 +272,9 @@ bool InitDriveThread::method_resizePartitions()
325
272
326
273
QProcess::execute (" /sbin/mlabel p:RECOVERY" );
327
274
328
- emit statusUpdate (tr (" Mounting FAT partition" ));
329
- if (!mountSystemPartition ())
330
- {
331
- emit error (tr (" Error mounting system partition." ));
332
- return false ;
333
- }
334
-
335
275
return true ;
336
276
}
337
277
338
- bool InitDriveThread::saveBootFiles ()
339
- {
340
- return QProcess::execute (" cp -a /mnt /tmp" ) == 0 ;
341
- }
342
-
343
- bool InitDriveThread::restoreBootFiles ()
344
- {
345
- bool status = QProcess::execute (" cp -a /tmp/mnt /" ) == 0 ;
346
- QProcess::execute (" rm -rf /tmp/mnt" );
347
- return status;
348
- }
349
-
350
278
int InitDriveThread::sizeofBootFilesInKB ()
351
279
{
352
280
QProcess proc;
@@ -397,52 +325,6 @@ bool InitDriveThread::zeroMbr()
397
325
&& QProcess::execute (" /bin/dd conv=fsync count=8 bs=512 if=/dev/zero seek=" +QString::number (sizeofSDCardInBlocks ()-8 )+" of=/dev/mmcblk0" ) == 0 ;
398
326
}
399
327
400
- bool InitDriveThread::partitionDrive ()
401
- {
402
- /* Partition layout:
403
- *
404
- * First 1MB (2048 blocks) kept empty for alignment
405
- * Followed by FAT partition of RESCUE_PARTITION_SIZE (default 1 GB)
406
- * Followed by extended partition spanning remainder of space
407
- * Followed by NOOBS persistent settings partition
408
- */
409
- QByteArray partitionTable;
410
- int rescueBlocks = RESCUE_PARTITION_SIZE*1024 *2 ;
411
-
412
- mbr_table extended_mbr;
413
- int startOfExtended = 2048 +rescueBlocks;
414
- int sizeOfDisk = getFileContents (" /sys/class/block/mmcblk0/size" ).trimmed ().toULongLong ();
415
- int sizeOfExtended = sizeOfDisk - startOfExtended - SETTINGS_PARTITION_SIZE;
416
- int startOfSettings = startOfExtended + sizeOfExtended;
417
-
418
- partitionTable = " 2048," +QByteArray::number (rescueBlocks)+" ,0E\n " ; /* FAT partition */
419
- partitionTable += QByteArray::number (startOfExtended)+" ," +QByteArray::number (sizeOfExtended)+" ,X\n " ; /* Extended partition with all remaining space */
420
- partitionTable += QByteArray::number (startOfSettings)+" ,,L\n " ; /* Settings partition */
421
- partitionTable += " 0,0\n " ;
422
-
423
- /* Write out empty extended partition table with signature */
424
- memset (&extended_mbr, 0 , sizeof extended_mbr);
425
- extended_mbr.signature [0 ] = 0x55 ;
426
- extended_mbr.signature [1 ] = 0xAA ;
427
- QFile f (" /dev/mmcblk0" );
428
- f.open (f.ReadWrite );
429
- f.seek (startOfExtended*512 );
430
- f.write ((char *) &extended_mbr, sizeof (extended_mbr));
431
- f.close ();
432
-
433
- /* Write main partition table */
434
- QString cmd = QString (" /sbin/sfdisk -uS /dev/mmcblk0" );
435
- QProcess proc;
436
- proc.setProcessChannelMode (proc.MergedChannels );
437
- proc.start (cmd);
438
- proc.write (partitionTable);
439
- proc.closeWriteChannel ();
440
- proc.waitForFinished (-1 );
441
- QThread::msleep (500 );
442
-
443
- return proc.exitCode () == 0 ;
444
- }
445
-
446
328
#ifdef RISCOS_BLOB_FILENAME
447
329
bool InitDriveThread::writeRiscOSblob ()
448
330
{
0 commit comments