summaryrefslogtreecommitdiff
path: root/common/cmd_fdisk.c
blob: d5b0c6b23892a0f54ede9a953e4db17dcaa89080 (plain)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528



#include <common.h>
#include <part.h>
//#include <config.h>
#include <command.h>
//#include <image.h>
//#include <linux/ctype.h>
//#include <asm/byteorder.h>
//#include <linux/stat.h>
//#include <malloc.h>

/* DOS partition types */

static const char *const i386_sys_types[] = {
	"\x00" "Empty",
	"\x01" "FAT12",
	"\x04" "FAT16 <32M",
	"\x05" "Extended",         /* DOS 3.3+ extended partition */
	"\x06" "FAT16",            /* DOS 16-bit >=32M */
	"\x07" "HPFS/NTFS",        /* OS/2 IFS, eg, HPFS or NTFS or QNX */
	"\x0a" "OS/2 Boot Manager",/* OS/2 Boot Manager */
	"\x0b" "Win95 FAT32",
	"\x0c" "Win95 FAT32 (LBA)",/* LBA really is 'Extended Int 13h' */
	"\x0e" "Win95 FAT16 (LBA)",
	"\x0f" "Win95 Ext'd (LBA)",
	"\x11" "Hidden FAT12",
	"\x12" "Compaq diagnostics",
	"\x14" "Hidden FAT16 <32M",
	"\x16" "Hidden FAT16",
	"\x17" "Hidden HPFS/NTFS",
	"\x1b" "Hidden Win95 FAT32",
	"\x1c" "Hidden W95 FAT32 (LBA)",
	"\x1e" "Hidden W95 FAT16 (LBA)",
	"\x3c" "Part.Magic recovery",
	"\x41" "PPC PReP Boot",
	"\x42" "SFS",
	"\x63" "GNU HURD or SysV", /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
	"\x80" "Old Minix",        /* Minix 1.4a and earlier */
	"\x81" "Minix / old Linux",/* Minix 1.4b and later */
	"\x82" "Linux swap",       /* also Solaris */
	"\x83" "Linux",
	"\x84" "OS/2 hidden C: drive",
	"\x85" "Linux extended",
	"\x86" "NTFS volume set",
	"\x87" "NTFS volume set",
	"\x8e" "Linux LVM",
	"\x9f" "BSD/OS",           /* BSDI */
	"\xa0" "Thinkpad hibernation",
	"\xa5" "FreeBSD",          /* various BSD flavours */
	"\xa6" "OpenBSD",
	"\xa8" "Darwin UFS",
	"\xa9" "NetBSD",
	"\xab" "Darwin boot",
	"\xb7" "BSDI fs",
	"\xb8" "BSDI swap",
	"\xbe" "Solaris boot",
	"\xeb" "BeOS fs",
	"\xee" "EFI GPT",                    /* Intel EFI GUID Partition Table */
	"\xef" "EFI (FAT-12/16/32)",         /* Intel EFI System Partition */
	"\xf0" "Linux/PA-RISC boot",         /* Linux/PA-RISC boot loader */
	"\xf2" "DOS secondary",              /* DOS 3.3+ secondary */
	"\xfd" "Linux raid autodetect",      /* New (2.2.x) raid partition with
						autodetect using persistent
						superblock */
#if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
	"\x02" "XENIX root",
	"\x03" "XENIX usr",
	"\x08" "AIX",              /* AIX boot (AIX -- PS/2 port) or SplitDrive */
	"\x09" "AIX bootable",     /* AIX data or Coherent */
	"\x10" "OPUS",
	"\x18" "AST SmartSleep",
	"\x24" "NEC DOS",
	"\x39" "Plan 9",
	"\x40" "Venix 80286",
	"\x4d" "QNX4.x",
	"\x4e" "QNX4.x 2nd part",
	"\x4f" "QNX4.x 3rd part",
	"\x50" "OnTrack DM",
	"\x51" "OnTrack DM6 Aux1", /* (or Novell) */
	"\x52" "CP/M",             /* CP/M or Microport SysV/AT */
	"\x53" "OnTrack DM6 Aux3",
	"\x54" "OnTrackDM6",
	"\x55" "EZ-Drive",
	"\x56" "Golden Bow",
	"\x5c" "Priam Edisk",
	"\x61" "SpeedStor",
	"\x64" "Novell Netware 286",
	"\x65" "Novell Netware 386",
	"\x70" "DiskSecure Multi-Boot",
	"\x75" "PC/IX",
	"\x93" "Amoeba",
	"\x94" "Amoeba BBT",       /* (bad block table) */
	"\xa7" "NeXTSTEP",
	"\xbb" "Boot Wizard hidden",
	"\xc1" "DRDOS/sec (FAT-12)",
	"\xc4" "DRDOS/sec (FAT-16 < 32M)",
	"\xc6" "DRDOS/sec (FAT-16)",
	"\xc7" "Syrinx",
	"\xda" "Non-FS data",
	"\xdb" "CP/M / CTOS / ...",/* CP/M or Concurrent CP/M or
	                              Concurrent DOS or CTOS */
	"\xde" "Dell Utility",     /* Dell PowerEdge Server utilities */
	"\xdf" "BootIt",           /* BootIt EMBRM */
	"\xe1" "DOS access",       /* DOS access or SpeedStor 12-bit FAT
	                              extended partition */
	"\xe3" "DOS R/O",          /* DOS R/O or SpeedStor */
	"\xe4" "SpeedStor",        /* SpeedStor 16-bit FAT extended
	                              partition < 1024 cyl. */
	"\xf1" "SpeedStor",
	"\xf4" "SpeedStor",        /* SpeedStor large partition */
	"\xfe" "LANstep",          /* SpeedStor >1024 cyl. or LANstep */
	"\xff" "BBT",              /* Xenix Bad Block Table */
#endif
	NULL
};

#define get_sys_types() i386_sys_types
#define MAXIMUM_PARTS 60

static const char *
partition_type(unsigned char type)
{
	int i;
	const char *const *types = get_sys_types();
	for (i = 0; types[i]; i++)
		if ((unsigned char)types[i][0] == type)
			return types[i] + 1;

	return "Unknown";
}


#define PRIMARY_PART 1
#define EXTENDED_PART 2
#define LOGICAL_PART 3
#define PRIMARY_MAX 4

int do_fdisk_add_primary(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
{
	int dev=0;
	int part=0;
	int address=0;//the unit is block size
	int length=0;//the unit is block size
	block_dev_desc_t *dev_desc=NULL;
	disk_partition_t info;
	char *ep;
	
	if (argc < 5) {
		printf ("Usage:\n%s\n", cmdtp->help);
		return(1);
	}
	dev = (int)simple_strtoul (argv[2], &ep, 16);
	dev_desc=get_dev(argv[1],dev);
	

	if (dev_desc == NULL) {
		printf ("\n** Block device %s %d not supported\n", argv[1], dev);
		return(1);
	}
	//printf("Using device %s %d, \n", argv[1], dev);


	if (*ep) {
		if (*ep != ':') {
			puts ("\n** Invalid boot device, use `dev[:part]' **\n");
			return 1;
		}
		part = (int)simple_strtoul(++ep, NULL, 16);
	}

	if ((part<=0)||(part> PRIMARY_MAX)) {
		printf("Please input a partition number which is from 1 to 4 !\n");
		return 1;
	}
	if (get_partition_info(dev_desc, part, &info)==0) {
		printf("The partition exists, please delete it first\n");
		return 1;
	}


	address = (int)simple_strtoul (argv[3], NULL, 16);
	if ((address<0)||(address>=dev_desc->lba)) {
		printf("Please input a validate block address which should be small then 0x%x !\n",
			dev_desc->lba);
		return 1;
	}
	length = (int)simple_strtoul (argv[4], NULL, 16);
	if ((length<=0)||
		(length>=dev_desc->lba)||
		((address+length)>dev_desc->lba)) {
		printf("Please input a validate size !\n");
		return 1;
	}
	
	return add_partition(dev_desc, part, PRIMARY_PART, address, length);
}


int do_fdisk_add_extended(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
{
	int dev=0;
	int part=0;
	int address=0;//the unit is block size
	int length=0;//the unit is block size
	block_dev_desc_t *dev_desc=NULL;
	disk_partition_t info;
	char *ep;
	
	if (argc < 5) {
		printf ("Usage:\n%s\n", cmdtp->help);
		return(1);
	}
	dev = (int)simple_strtoul (argv[2], &ep, 16);
	dev_desc=get_dev(argv[1],dev);
	

	if (dev_desc == NULL) {
		printf ("\n** Block device %s %d not supported\n", argv[1], dev);
		return(1);
	}
	//printf("Using device %s %d, \n", argv[1], dev);

	
	if (*ep) {
		if (*ep != ':') {
			puts ("\n** Invalid boot device, use `dev[:part]' **\n");
			return 1;
		}
		part = (int)simple_strtoul(++ep, NULL, 16);
	}
	
	if ((part<=0)||(part> PRIMARY_MAX)) {
		printf("Please input a partition number which is from 1 to 4 !\n");
		return 1;
	}
	if (get_partition_info(dev_desc, part, &info)==0) {
		printf("The partition exists, please delete it first\n");
		return 1;
	}

	address = (int)simple_strtoul (argv[3], NULL, 16);
	if ((address<=0)||(address>=dev_desc->lba)) {
		printf("Please input a validate block address which should be small then 0x%x !\n",
			dev_desc->lba);
		return 1;
	}
	length = (int)simple_strtoul (argv[4], NULL, 16);
	if ((length<=0)||
		(length>=dev_desc->lba)||
		((address+length)>dev_desc->lba)) {
		printf("Please input a validate size !\n");
		return 1;
	}
	
	return add_partition(dev_desc, part, EXTENDED_PART, address, length);
}

int do_fdisk_add_logical(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
{
	int dev=0;
	int address=0;//the unit is block size
	int length=0;//the unit is block size
	block_dev_desc_t *dev_desc=NULL;
	
	if (argc < 5) {
		printf ("Usage:\n%s\n", cmdtp->help);
		return(1);
	}
	dev = (int)simple_strtoul (argv[2], NULL, 16);
	dev_desc=get_dev(argv[1],dev);
	

	if (dev_desc == NULL) {
		printf ("\n** Block device %s %d not supported\n", argv[1], dev);
		return(1);
	}
	printf("Using device %s %d, \n", argv[1], dev);

	address = (int)simple_strtoul (argv[3], NULL, 16);
	if ((address<=0)||(address>=dev_desc->lba)) {
		printf("Please input a validate block address which should be small then 0x%x !\n",
			dev_desc->lba);
		return 1;
	}
	length = (int)simple_strtoul (argv[4], NULL, 16);
	if ((length<=0)||
		(length>=dev_desc->lba)||
		((address+length)>dev_desc->lba)) {
		printf("Please input a validate size !\n");
		return 1;
	}
	
	return add_partition(dev_desc, 1, LOGICAL_PART, address, length);
}

int do_fdisk_del(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
{
	int dev=0;
	int part=0;
	block_dev_desc_t *dev_desc=NULL;
	char *ep;

	if (argc < 3) {
		printf ("Usage:\n%s\n", cmdtp->help);
		return(1);
	}
	dev = (int)simple_strtoul (argv[2], &ep, 16);
	dev_desc=get_dev(argv[1],dev);

	if (dev_desc == NULL) {
		printf ("\n** Block device %s %d not supported\n", argv[1], dev);
		return(1);
	}
	if (*ep) {
		if (*ep != ':') {
			puts ("\n** Invalid boot device, use `dev[:part]' **\n");
			return 1;
		}
		part = (int)simple_strtoul(++ep, NULL, 16);
	}

	if (part<=0) {
		printf("Please input a partition number which is from 1 !\n");
		return 1;
	}
	if (del_partition(dev_desc, part)==0)
		return 0;
	else 
		return 1;
}

int do_fdisk_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
{
	int dev=0;
	int part=1;
	block_dev_desc_t *dev_desc=NULL;
	disk_partition_t info[MAXIMUM_PARTS];

	if (argc < 3) {
		printf ("Usage:\n%s\n", cmdtp->help);
		return(1);
	}
	dev = (int)simple_strtoul (argv[2], NULL, 16);
	dev_desc=get_dev(argv[1],dev);

	if (dev_desc == NULL) {
		printf ("\n** Block device %s %d not supported\n", argv[1], dev);
		return(1);
	}

	//Show the total size in the Device
	printf("Total size(blocks): 0x%x, the block size(byte): 0x%x\n", dev_desc->lba, 
		dev_desc->blksz);
	//show the partitions in the MBR
	for(part=1; part<5; part++) {
		if (get_partition_info(dev_desc, part, &info[part])==0) {
			printf("----------------------------------\n");
			printf("Partition: %d\n", part);
			printf("Type: %s\n", info[part].type);
			printf("System: %s\n", partition_type((unsigned char)(simple_strtoul (info[part].type, NULL, 10))));
			printf("Start from (block): 0x%x\n", info[part].start);
			printf("End of (block): 0x%x\n", info[part].start+info[part].size-1);
			printf("Size (blocks): 0x%x\n", info[part].size);
			printf("Offset (blocks): 0x%x\n", info[part].offset);			
			printf("total size (Kbytes): %d\n", info[part].size>>1);
			printf("----------------------------------\n");
		}
	}

	//show the logical partitions
	while(get_partition_info(dev_desc, part, &info[part])==0){
		printf("----------------------------------\n");
		printf("Partition: %d\n", part);
		printf("Type: %s\n", info[part].type);
		printf("System: %s\n", partition_type((unsigned char)(simple_strtoul (info[part].type, NULL, 10))));
		printf("Start from (block): 0x%x\n", info[part].start);		
		printf("End of (block): 0x%x\n", info[part].start+info[part].size-1);
		printf("Size (blocks): 0x%x\n", info[part].size);
		printf("Offset (blocks): 0x%x\n", info[part].offset);			
		printf("total size (Kbytes): %d\n", info[part].size>>1);
		printf("----------------------------------\n");
		part++;
	}

	return 0;
}
#define MAXPARTS 15//for now 15 parts is enough
#define MAXSIZE 5//for now each part is less than 99999M
static int findParts(const char *parts, int *partslist)
{
    int i=0, j=0;
    char onePart[10];
    int onePartSize=0;
    int lastComma=0;
    int temp=0;
    disk_partition_t info[MAXIMUM_PARTS];
    while(*(parts+i)!=NULL) {
        if ((*(parts+i)>=48)&&(*(parts+i)<=57)) {
            onePartSize++;
	}else {
            if(*(parts+i)==',')
                lastComma=i;//we want to remember the position of the last comma, 
                                   //since next after the last comma is userdata.
            if (onePartSize!=0) {
			memcpy(onePart, parts+i-onePartSize, onePartSize);
			partslist[j] = (int)simple_strtoul (onePart, NULL, 10);
			memset(onePart, 0, sizeof(onePart));
			onePartSize=0;
			j++;
		}
	}
	i++;
    }

    if(*(parts+lastComma+1)=='-') {
        partslist[j]=0xffffffff;
	 j++;
    } 
	
    for (i=0;i<j;i++)
    	printf("dump the partslist is: %d\n", partslist[i]);
    printf("there are %d pars\n",j);
    return j;//return how many parts
}
void do_fdisk_create(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
{
    int partlist[MAXPARTS], address[MAXPARTS], length[MAXPARTS];
    int dev=0;
    int partNum=0;
    int i=0;
    int totalSize=0;
    disk_partition_t info[MAXIMUM_PARTS];
    block_dev_desc_t *dev_desc=NULL;
    if (argc < 4) {
        printf ("Usage:\n%s\n", cmdtp->help);
        return(1);
    }
    dev = (int)simple_strtoul (argv[2], NULL, 16);
    dev_desc=get_dev(argv[1],dev);

    if (dev_desc == NULL) {
        printf ("\n** Block device %s %d not supported\n", argv[1], dev);
        return(1);
    }
    memset(partlist, 0, sizeof(partlist));
    // 1. get the partlist which we want to set
    partNum=findParts(argv[3], partlist);
	
    // 2. find the address and length of each part.
    getUserOrderParam(dev_desc, partlist, address, length, partNum);

    // 3. compare the ori and new parts to see whether have to do the update... 
    for(i=0; i<partNum; i++) {
        if (get_partition_info(dev_desc, i+1, &info[i])!=0) {
            printf("Cannot get the partition info \n");
            break;
        } else {
            if (info[i].size!= length[i]) {
               printf("For part %d, the ori part size is 0x%x blocks, but the user want 0x%x blocks\n",
           	    i+1, info[i].size, length[i]);
                break;
            }
        }
    }
	
    if (i==partNum) {
        printf("No need to update the parts, since it's the same as you want\n");
        return 0;
    } 

		
	
    printf("Updating the parts\n");

    // 4. delete the all parts, first
    for(i=PRIMARY_MAX;i>0;i--) {
        del_partition(dev_desc, i);
    }

    // 5. update the parts
    for (i=0;i<partNum;i++) {
        if (i<LOGICAL_PART) {
            add_partition(dev_desc, i+1, PRIMARY_PART, address[i], length[i]);
        } else if (i==LOGICAL_PART){
            add_partition(dev_desc, i+1, EXTENDED_PART, address[i], length[i]);
        } else {
            add_partition(dev_desc, 1, LOGICAL_PART, address[i], length[i]);
        }
    }

    return 0;		
	
}


U_BOOT_CMD(fdiskaddp, 5, 1, do_fdisk_add_primary,
	"add a primary partition in the storage device",
	"<interface> <dev:part> <address(hex)> <length(hex)>\n"
	"     - add a primary partition in the storage device");

U_BOOT_CMD(fdiskadde, 5, 1, do_fdisk_add_extended,
	"add a extended partition in the storage device",
	"<interface> <dev:part> <address(hex)> <length(hex)>\n"
	"     - add a extended partition in the storage device");

U_BOOT_CMD(fdiskaddl, 5, 1, do_fdisk_add_logical,
	"add a logical partition in the storage device",
	"<interface> <dev> <address(hex)> <length(hex)>\n"
	"     - add a logical partition in the storage device");


U_BOOT_CMD(fdiskdel, 3, 1, do_fdisk_del,
	"delete a partition in the storage device",
	"<interface> <dev:part> \n"
	"     - delete the partition in the storage device");

U_BOOT_CMD(fdiskls, 3, 1, do_fdisk_ls,
	"list the partitions in the storage device",
	"<interface> <dev> \n"
	"	  - list all the partiitons in the storage device");

U_BOOT_CMD(fdisk_create, 4, 1, do_fdisk_create,
	"create the partitions in the storage device according to the parts",
	"<interface> <dev> <parts>\n"
	"	  - create all the partiitons in the storage device accroding to the parameters");