summaryrefslogtreecommitdiffstats
path: root/src/add-ons/kernel/partitioning_systems/gpt/efi_gpt.cpp
blob: ad8f784bca43eed0f94db4ba9c924d56af8d768b (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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
/*
 * Copyright 2009, Michael Lotz, mmlr@mlotz.ch. All rights reserved.
 * Copyright 2007-2013, Axel Dörfler, axeld@pinc-software.de.
 *
 * Distributed under the terms of the MIT License.
 */


#include "efi_gpt.h"

#include <KernelExport.h>
#include <disk_device_manager/ddm_modules.h>
#include <disk_device_types.h>
#ifdef _BOOT_MODE
#	include <boot/partitions.h>
#else
#	include <DiskDeviceTypes.h>
#	include "PartitionLocker.h"
#endif
#include <util/kernel_cpp.h>

#include <unistd.h>
#include <stdio.h>
#include <string.h>

#include "Header.h"
#include "utility.h"


#define TRACE_EFI_GPT
#ifdef TRACE_EFI_GPT
#	define TRACE(x) dprintf x
#else
#	define TRACE(x) ;
#endif


#define EFI_PARTITION_MODULE_NAME "partitioning_systems/efi_gpt/v1"


#ifndef _BOOT_MODE
static off_t
block_align(partition_data* partition, off_t offset, bool upwards)
{
	// Take HDs into account that hide the fact they are using a
	// block size of 4096 bytes, and round to that.
	uint32 blockSize = max_c(partition->block_size, 4096);
	if (upwards)
		return ((offset + blockSize - 1) / blockSize) * blockSize;

	return (offset / blockSize) * blockSize;
}
#endif // !_BOOT_MODE


//	#pragma mark - public module interface


static status_t
efi_gpt_std_ops(int32 op, ...)
{
	switch (op) {
		case B_MODULE_INIT:
		case B_MODULE_UNINIT:
			return B_OK;
	}

	return B_ERROR;
}


static float
efi_gpt_identify_partition(int fd, partition_data* partition, void** _cookie)
{
	EFI::Header* header = new (std::nothrow) EFI::Header(fd,
		(partition->size - 1) / partition->block_size, partition->block_size);
	status_t status = header->InitCheck();
	if (status != B_OK) {
		delete header;
		return -1;
	}

	*_cookie = header;
	return 0.96;
		// This must be higher as Intel partitioning, as EFI can contain this
		// partitioning for compatibility
}


static status_t
efi_gpt_scan_partition(int fd, partition_data* partition, void* _cookie)
{
	TRACE(("efi_gpt_scan_partition(cookie = %p)\n", _cookie));
	EFI::Header* header = (EFI::Header*)_cookie;

	partition->status = B_PARTITION_VALID;
	partition->flags |= B_PARTITION_PARTITIONING_SYSTEM;
	partition->content_size = partition->size;
	partition->content_cookie = header;

	// scan all children

	uint32 index = 0;

	for (uint32 i = 0; i < header->EntryCount(); i++) {
		const efi_partition_entry& entry = header->EntryAt(i);

		if (entry.partition_type == kEmptyGUID)
			continue;

		if (entry.EndBlock() * partition->block_size
				> (uint64)partition->size) {
			TRACE(("efi_gpt: child partition exceeds existing space (ends at "
				"block %" B_PRIu64 ")\n", entry.EndBlock()));
			continue;
		}

		partition_data* child = create_child_partition(partition->id, index++,
			partition->offset + entry.StartBlock() * partition->block_size,
			entry.BlockCount() * partition->block_size, -1);
		if (child == NULL) {
			TRACE(("efi_gpt: Creating child at index %" B_PRIu32 " failed\n",
				index - 1));
			return B_ERROR;
		}

		char name[B_OS_NAME_LENGTH];
		to_utf8(entry.name, EFI_PARTITION_NAME_LENGTH, name, sizeof(name));
		child->name = strdup(name);
		child->type = strdup(get_partition_type(entry.partition_type));
		child->block_size = partition->block_size;
		child->cookie = (void*)(addr_t)i;
	}

	return B_OK;
}


static void
efi_gpt_free_identify_partition_cookie(partition_data* partition, void* _cookie)
{
	// Cookie is freed in efi_gpt_free_partition_content_cookie().
}


static void
efi_gpt_free_partition_content_cookie(partition_data* partition)
{
	delete (EFI::Header*)partition->content_cookie;
}


#ifndef _BOOT_MODE
static uint32
efi_gpt_get_supported_operations(partition_data* partition, uint32 mask)
{
	uint32 flags = B_DISK_SYSTEM_SUPPORTS_INITIALIZING
		| B_DISK_SYSTEM_SUPPORTS_SETTING_CONTENT_NAME
		| B_DISK_SYSTEM_SUPPORTS_MOVING
		| B_DISK_SYSTEM_SUPPORTS_RESIZING
		| B_DISK_SYSTEM_SUPPORTS_CREATING_CHILD;
		// TODO: check for available entries and partitionable space and only
		// add creating child support if both is valid

	return flags;
}


static uint32
efi_gpt_get_supported_child_operations(partition_data* partition,
	partition_data* child, uint32 mask)
{
	return B_DISK_SYSTEM_SUPPORTS_MOVING_CHILD
		| B_DISK_SYSTEM_SUPPORTS_RESIZING_CHILD
		| B_DISK_SYSTEM_SUPPORTS_SETTING_TYPE
		| B_DISK_SYSTEM_SUPPORTS_DELETING_CHILD;
}


static bool
efi_gpt_is_sub_system_for(partition_data* partition)
{
	// a GUID Partition Table doesn't usually live inside another partition
	return false;
}


static bool
efi_gpt_validate_resize(partition_data* partition, off_t* size)
{
	off_t newSize = *size;
	if (newSize == partition->size)
		return true;

	if (newSize < 0)
		newSize = 0;
	else
		newSize = block_align(partition, newSize, false);

	// growing
	if (newSize > partition->size) {
		*size = newSize;
		return true;
	}

	// shrinking, only so that no child would be truncated
	off_t newEnd = partition->offset + newSize;
	for (int32 i = 0; i < partition->child_count; i++) {
		partition_data* child = get_child_partition(partition->id, i);
		if (child == NULL)
			continue;

		if (child->offset + child->size > newEnd)
			newEnd = child->offset + child->size;
	}

	newSize = block_align(partition, newEnd - partition->offset, true);
	*size = newSize;
	return true;
}


static bool
efi_gpt_validate_resize_child(partition_data* partition, partition_data* child,
	off_t* size)
{
	off_t newSize = *size;
	if (newSize == child->size)
		return true;

	// shrinking
	if (newSize < child->size) {
		if (newSize < 0)
			newSize = 0;

		*size = block_align(partition, newSize, false);
		return true;
	}

	// growing, but only so much that the child doesn't get bigger than
	// the parent
	if (child->offset + newSize > partition->offset + partition->size)
		newSize = partition->offset + partition->size - child->offset;

	// make sure that the child doesn't overlap any sibling partitions
	off_t newEnd = child->offset + newSize;
	for (int32 i = 0; i < partition->child_count; i++) {
		partition_data* other = get_child_partition(partition->id, i);
		if (other == NULL || other->id == child->id
			|| other->offset < child->offset)
			continue;

		if (newEnd > other->offset)
			newEnd = other->offset;
	}

	*size = block_align(partition, newEnd - child->offset, false);
	return true;
}


static bool
efi_gpt_validate_move(partition_data* partition, off_t* start)
{
	// nothing to do
	return true;
}


static bool
efi_gpt_validate_move_child(partition_data* partition, partition_data* child,
	off_t* start)
{
	off_t newStart = *start;
	if (newStart < 0)
		newStart = 0;

	if (newStart + child->size > partition->size)
		newStart = partition->size - child->size;

	newStart = block_align(partition, newStart, false);
	if (newStart > child->offset) {
		for (int32 i = 0; i < partition->child_count; i++) {
			partition_data* other = get_child_partition(partition->id, i);
			if (other == NULL || other->id == child->id
				|| other->offset < child->offset)
				continue;

			if (other->offset < newStart + child->size)
				newStart = other->offset - child->size;
		}

		newStart = block_align(partition, newStart, false);
	} else {
		for (int32 i = 0; i < partition->child_count; i++) {
			partition_data* other = get_child_partition(partition->id, i);
			if (other == NULL || other->id == child->id
				|| other->offset > child->offset)
				continue;

			if (other->offset + other->size > newStart)
				newStart = other->offset + other->size;
		}

		newStart = block_align(partition, newStart, true);
	}

	*start = newStart;
	return true;
}


static bool
efi_gpt_validate_set_content_name(partition_data* partition, char* name)
{
	// TODO: should validate that the utf-8 -> ucs-2 is valid
	// TODO: should count actual utf-8 chars
	if (strlen(name) > EFI_PARTITION_NAME_LENGTH)
		name[EFI_PARTITION_NAME_LENGTH - 1] = 0;
	return true;
}


static bool
efi_gpt_validate_set_type(partition_data* partition, const char* type)
{
	guid_t typeGUID;
	return get_guid_for_partition_type(type, typeGUID);
}


static bool
efi_gpt_validate_initialize(partition_data* partition, char* name,
	const char* parameters)
{
	if ((efi_gpt_get_supported_operations(partition, ~0)
		& B_DISK_SYSTEM_SUPPORTS_INITIALIZING) == 0)
		return false;

	// name and parameters are ignored
	if (name != NULL)
		name[0] = 0;

	return true;
}


static bool
efi_gpt_validate_create_child(partition_data* partition, off_t* start,
	off_t* size, const char* type, const char* name, const char* parameters,
	int32* index)
{
	if ((efi_gpt_get_supported_operations(partition, ~0)
			& B_DISK_SYSTEM_SUPPORTS_CREATING_CHILD) == 0)
		return false;

	if (!efi_gpt_validate_set_type(partition, type))
		return false;

	EFI::Header* header = (EFI::Header*)partition->content_cookie;
	int32 entryIndex = -1;
	for (uint32 i = 0; i < header->EntryCount(); i++) {
		const efi_partition_entry& entry = header->EntryAt(i);
		if (entry.partition_type == kEmptyGUID) {
			entryIndex = i;
			break;
		}
	}

	if (entryIndex < 0)
		return false;

	*index = entryIndex;

	// ensure that child lies between first and last usable block
	off_t firstUsable = header->FirstUsableBlock() * partition->block_size;
	if (*start < firstUsable)
		*start = firstUsable;

	off_t lastUsable = header->LastUsableBlock() * partition->block_size;
	if (*start + *size > lastUsable) {
		if (*start > lastUsable)
			return false;

		*size = lastUsable - *start;
	}

	// ensure that we don't overlap any siblings
	for (int32 i = 0; i < partition->child_count; i++) {
		partition_data* other = get_child_partition(partition->id, i);
		if (other == NULL)
			continue;

		if (other->offset < *start && other->offset + other->size > *start)
			*start = other->offset + other->size;

		if (other->offset > *start && other->offset < *start + *size)
			*size = other->offset - *start;
	}

	*start = block_align(partition, *start, true);
	*size = block_align(partition, *size, false);

	// TODO: support parameters
	return true;
}


static status_t
efi_gpt_get_partitionable_spaces(partition_data* partition,
	partitionable_space_data* buffer, int32 count, int32* actualCount)
{
	// TODO: implement
	return B_ERROR;
}


static status_t
efi_gpt_get_next_supported_type(partition_data* partition, int32* cookie,
	char* type)
{
	// TODO: implement
	return B_ERROR;
}


static status_t
efi_gpt_shadow_changed(partition_data* partition, partition_data* child,
	uint32 operation)
{
	// TODO: implement
	return B_ERROR;
}


static status_t
efi_gpt_repair(int fd, partition_id partition, bool checkOnly, disk_job_id job)
{
	// TODO: implement, validate CRCs and restore from backup area if corrupt
	return B_ERROR;
}


static status_t
efi_gpt_resize(int fd, partition_id partitionID, off_t size, disk_job_id job)
{
	if (fd < 0)
		return B_ERROR;

	PartitionWriteLocker locker(partitionID);
	if (!locker.IsLocked())
		return B_ERROR;

	partition_data* partition = get_partition(partitionID);
	if (partition == NULL)
		return B_BAD_VALUE;

	off_t validatedSize = size;
	if (!efi_gpt_validate_resize(partition, &validatedSize))
		return B_BAD_VALUE;

	update_disk_device_job_progress(job, 0.0);

	partition->size = validatedSize;
	partition->content_size = validatedSize;

	update_disk_device_job_progress(job, 1.0);
	partition_modified(partitionID);
	return B_OK;
}


static status_t
efi_gpt_resize_child(int fd, partition_id partitionID, off_t size,
	disk_job_id job)
{
	if (fd < 0)
		return B_ERROR;

	PartitionWriteLocker locker(partitionID);
	if (!locker.IsLocked())
		return B_ERROR;

	partition_data* child = get_partition(partitionID);
	if (child == NULL)
		return B_BAD_VALUE;

	partition_data* partition = get_parent_partition(partitionID);
	if (partition == NULL)
		return B_BAD_VALUE;

	EFI::Header* header = (EFI::Header*)partition->content_cookie;
	if (header == NULL)
		return B_BAD_VALUE;

	uint32 entryIndex = (uint32)(addr_t)child->cookie;
	if (entryIndex >= header->EntryCount())
		return B_BAD_VALUE;

	off_t validatedSize = size;
	if (!efi_gpt_validate_resize_child(partition, child, &validatedSize))
		return B_BAD_VALUE;

	if (child->size == validatedSize)
		return B_OK;

	update_disk_device_job_progress(job, 0.0);

	efi_partition_entry& entry = header->EntryAt(entryIndex);
	entry.SetBlockCount(validatedSize / partition->block_size);

	status_t result = header->WriteEntry(fd, entryIndex);
	if (result != B_OK) {
		entry.SetBlockCount(child->size / partition->block_size);
		return result;
	}

	child->size = validatedSize;

	update_disk_device_job_progress(job, 1.0);
	partition_modified(partitionID);
	return B_OK;
}


static status_t
efi_gpt_move(int fd, partition_id partition, off_t offset, disk_job_id job)
{
	// nothing to do here
	return B_OK;
}


static status_t
efi_gpt_move_child(int fd, partition_id partitionID, partition_id childID,
	off_t offset, disk_job_id job)
{
	if (fd < 0)
		return B_ERROR;

	PartitionWriteLocker locker(partitionID);
	if (!locker.IsLocked())
		return B_ERROR;

	partition_data* partition = get_partition(partitionID);
	if (partition == NULL)
		return B_BAD_VALUE;

	partition_data* child = get_partition(childID);
	if (child == NULL)
		return B_BAD_VALUE;

	EFI::Header* header = (EFI::Header*)partition->content_cookie;
	if (header == NULL)
		return B_BAD_VALUE;

	uint32 entryIndex = (uint32)(addr_t)child->cookie;
	if (entryIndex >= header->EntryCount())
		return B_BAD_VALUE;

	off_t validatedOffset = offset;
	if (!efi_gpt_validate_move_child(partition, child, &validatedOffset))
		return B_BAD_VALUE;

	if (child->offset == validatedOffset)
		return B_OK;

	// TODO: implement actual moving, need to move the partition content
	// (the raw data) here and need to take overlap into account
	return B_ERROR;

	update_disk_device_job_progress(job, 0.0);

	efi_partition_entry& entry = header->EntryAt(entryIndex);
	uint64 blockCount = entry.BlockCount();
	entry.SetStartBlock((validatedOffset - partition->offset)
		/ partition->block_size);
	entry.SetBlockCount(blockCount);

	status_t result = header->WriteEntry(fd, entryIndex);
	if (result != B_OK) {
		// fatal error: the data has been moved but the partition table could
		// not be updated to reflect that change!
		return result;
	}

	child->offset = validatedOffset;

	update_disk_device_job_progress(job, 1.0);
	partition_modified(childID);
	return B_OK;
}


static status_t
efi_gpt_set_content_name(int fd, partition_id partitionID, const char* name,
	disk_job_id job)
{
	if (fd < 0)
		return B_ERROR;

	PartitionWriteLocker locker(partitionID);
	if (!locker.IsLocked())
		return B_ERROR;

	partition_data* child = get_partition(partitionID);
	if (child == NULL)
		return B_BAD_VALUE;

	partition_data* partition = get_parent_partition(partitionID);
	if (partition == NULL)
		return B_BAD_VALUE;

	EFI::Header* header = (EFI::Header*)partition->content_cookie;
	if (header == NULL)
		return B_BAD_VALUE;

	uint32 entryIndex = (uint32)(addr_t)child->cookie;
	if (entryIndex >= header->EntryCount())
		return B_BAD_VALUE;

	update_disk_device_job_progress(job, 0.0);

	efi_partition_entry& entry = header->EntryAt(entryIndex);
	to_ucs2(name, strlen(name), entry.name, EFI_PARTITION_NAME_LENGTH);

	status_t result = header->WriteEntry(fd, entryIndex);
	if (result != B_OK)
		return result;

	char newName[B_OS_NAME_LENGTH];
	to_utf8(entry.name, EFI_PARTITION_NAME_LENGTH, newName, sizeof(newName));
	child->name = strdup(newName);

	update_disk_device_job_progress(job, 1.0);
	partition_modified(partitionID);
	return B_OK;
}


static status_t
efi_gpt_set_type(int fd, partition_id partitionID, const char* type,
	disk_job_id job)
{
	if (fd < 0)
		return B_ERROR;

	PartitionWriteLocker locker(partitionID);
	if (!locker.IsLocked())
		return B_ERROR;

	partition_data* child = get_partition(partitionID);
	if (child == NULL)
		return B_BAD_VALUE;

	partition_data* partition = get_parent_partition(partitionID);
	if (partition == NULL)
		return B_BAD_VALUE;

	EFI::Header* header = (EFI::Header*)partition->content_cookie;
	if (header == NULL)
		return B_BAD_VALUE;

	uint32 entryIndex = (uint32)(addr_t)child->cookie;
	if (entryIndex >= header->EntryCount())
		return B_BAD_VALUE;

	guid_t typeGUID;
	if (!get_guid_for_partition_type(type, typeGUID))
		return B_BAD_VALUE;

	update_disk_device_job_progress(job, 0.0);

	efi_partition_entry& entry = header->EntryAt(entryIndex);
	entry.partition_type = typeGUID;

	status_t result = header->WriteEntry(fd, entryIndex);
	if (result != B_OK)
		return result;

	child->type = strdup(type);

	update_disk_device_job_progress(job, 1.0);
	partition_modified(partitionID);
	return B_OK;
}


static status_t
efi_gpt_initialize(int fd, partition_id partitionID, const char* name,
	const char* parameters, off_t partitionSize, disk_job_id job)
{
	if (fd < 0)
		return B_ERROR;

	partition_data* partition = get_partition(partitionID);
	if (partition == NULL)
		return B_BAD_VALUE;

	update_disk_device_job_progress(job, 0.0);

	EFI::Header header((partitionSize - 1) / partition->block_size,
		partition->block_size);
	status_t result = header.InitCheck();
	if (result != B_OK)
		return result;

	result = header.Write(fd);
	if (result != B_OK)
		return result;

	result = scan_partition(partitionID);
	if (result != B_OK)
		return result;

	update_disk_device_job_progress(job, 1.0);
	partition_modified(partitionID);
	return B_OK;
}


static status_t
efi_gpt_create_child(int fd, partition_id partitionID, off_t offset,
	off_t size, const char* type, const char* name, const char* parameters,
	disk_job_id job, partition_id* childID)
{
	if (fd < 0)
		return B_ERROR;

	PartitionWriteLocker locker(partitionID);
	if (!locker.IsLocked())
		return B_ERROR;

	partition_data* partition = get_partition(partitionID);
	if (partition == NULL)
		return B_BAD_VALUE;

	EFI::Header* header = (EFI::Header*)partition->content_cookie;
	if (header == NULL)
		return B_BAD_VALUE;

	off_t validatedOffset = offset;
	off_t validatedSize = size;
	uint32 entryIndex = 0;

	if (!efi_gpt_validate_create_child(partition, &validatedOffset,
			&validatedSize, type, name, parameters, (int32*)&entryIndex))
		return B_BAD_VALUE;

	guid_t typeGUID;
	if (!get_guid_for_partition_type(type, typeGUID))
		return B_BAD_VALUE;

	update_disk_device_job_progress(job, 0.0);

	partition_data* child = create_child_partition(partition->id, entryIndex,
		validatedOffset, validatedSize, *childID);
	if (child == NULL)
		return B_ERROR;

	efi_partition_entry& entry = header->EntryAt(entryIndex);
	entry.partition_type = typeGUID;
	// TODO: set unique partition ID
	to_ucs2(name, strlen(name), entry.name, EFI_PARTITION_NAME_LENGTH);
	entry.SetStartBlock((validatedOffset - partition->offset)
		/ partition->block_size);
	entry.SetBlockCount(validatedSize / partition->block_size);
	entry.SetAttributes(0); // TODO

	status_t result = header->WriteEntry(fd, entryIndex);
	if (result != B_OK) {
		delete_partition(child->id);
		return result;
	}

	*childID = child->id;
	child->block_size = partition->block_size;
	child->name = strdup(name);
	child->type = strdup(type);
	child->parameters = strdup(parameters);
	child->cookie = (void*)(addr_t)entryIndex;

	if (child->type == NULL || child->parameters == NULL) {
		delete_partition(child->id);
		return B_NO_MEMORY;
	}

	update_disk_device_job_progress(job, 1.0);
	partition_modified(partitionID);
	return B_OK;
}


static status_t
efi_gpt_delete_child(int fd, partition_id partitionID, partition_id childID,
	disk_job_id job)
{
	if (fd < 0)
		return B_ERROR;

	PartitionWriteLocker locker(partitionID);
	if (!locker.IsLocked())
		return B_ERROR;

	partition_data* partition = get_partition(partitionID);
	if (partition == NULL)
		return B_BAD_VALUE;

	partition_data* child = get_partition(childID);
	if (child == NULL)
		return B_BAD_VALUE;

	EFI::Header* header = (EFI::Header*)partition->content_cookie;
	if (header == NULL)
		return B_BAD_VALUE;

	uint32 entryIndex = (uint32)(addr_t)child->cookie;
	if (entryIndex >= header->EntryCount())
		return B_BAD_VALUE;

	update_disk_device_job_progress(job, 0.0);

	if (!delete_partition(childID))
		return B_ERROR;

	efi_partition_entry& entry = header->EntryAt(entryIndex);
	entry.partition_type = kEmptyGUID;

	status_t result = header->WriteEntry(fd, entryIndex);
	if (result != B_OK)
		return result;

	update_disk_device_job_progress(job, 1.0);
	partition_modified(partitionID);
	return B_OK;
}
#endif // !_BOOT_MODE


#ifndef _BOOT_MODE
static partition_module_info sEFIPartitionModule = {
#else
partition_module_info gEFIPartitionModule = {
#endif
	{
		EFI_PARTITION_MODULE_NAME,
		0,
		efi_gpt_std_ops
	},
	"gpt",									// short_name
	EFI_PARTITION_NAME,						// pretty_name
	0										// flags
	| B_DISK_SYSTEM_SUPPORTS_INITIALIZING
	| B_DISK_SYSTEM_SUPPORTS_MOVING
	| B_DISK_SYSTEM_SUPPORTS_RESIZING
	| B_DISK_SYSTEM_SUPPORTS_SETTING_TYPE
	| B_DISK_SYSTEM_SUPPORTS_CONTENT_NAME
	| B_DISK_SYSTEM_SUPPORTS_SETTING_CONTENT_NAME
	| B_DISK_SYSTEM_SUPPORTS_MOVING_CHILD
	| B_DISK_SYSTEM_SUPPORTS_RESIZING_CHILD
	| B_DISK_SYSTEM_SUPPORTS_CREATING_CHILD
	| B_DISK_SYSTEM_SUPPORTS_DELETING_CHILD
	,

	// scanning
	efi_gpt_identify_partition,
	efi_gpt_scan_partition,
	efi_gpt_free_identify_partition_cookie,
	NULL, // free_partition_cookie
	efi_gpt_free_partition_content_cookie,

#ifndef _BOOT_MODE
	// querying
	efi_gpt_get_supported_operations,
	efi_gpt_get_supported_child_operations,
	NULL, // supports_initializing_child
	efi_gpt_is_sub_system_for,

	efi_gpt_validate_resize,
	efi_gpt_validate_resize_child,
	efi_gpt_validate_move,
	efi_gpt_validate_move_child,
	NULL, // validate_set_name
	efi_gpt_validate_set_content_name,
	efi_gpt_validate_set_type,
	NULL, // validate_set_parameters
	NULL, // validate_set_content_parameters
	efi_gpt_validate_initialize,
	efi_gpt_validate_create_child,
	efi_gpt_get_partitionable_spaces,
	efi_gpt_get_next_supported_type,
	NULL, // get_type_for_content_type

	// shadow partition modification
	efi_gpt_shadow_changed,

	// writing
	efi_gpt_repair,
	efi_gpt_resize,
	efi_gpt_resize_child,
	efi_gpt_move,
	efi_gpt_move_child,
	NULL, // set_name
	efi_gpt_set_content_name,
	efi_gpt_set_type,
	NULL, // set_parameters
	NULL, // set_content_parameters
	efi_gpt_initialize,
	NULL, // uninitialize
	efi_gpt_create_child,
	efi_gpt_delete_child
#else
	NULL
#endif // _BOOT_MODE
};

#ifndef _BOOT_MODE
partition_module_info* modules[] = {
	&sEFIPartitionModule,
	NULL
};
#endif