Reactos
1/*
2 * PROJECT: ReactOS DiskPart
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/system/diskpart/setid.c
5 * PURPOSE: Manages all the partitions of the OS in an interactive way.
6 * PROGRAMMERS: Lee Schroeder
7 */
8
9#include "diskpart.h"
10
11#define NDEBUG
12#include <debug.h>
13
14
15EXIT_CODE
16setid_main(
17 _In_ INT argc,
18 _In_ PWSTR *argv)
19{
20 INT i, length;
21 PWSTR pszSuffix, pszId = NULL;
22 NTSTATUS Status;
23
24 DPRINT("SetId()\n");
25
26 if (CurrentDisk == NULL)
27 {
28 ConResPuts(StdOut, IDS_SELECT_NO_DISK);
29 return EXIT_SUCCESS;
30 }
31
32 if (CurrentPartition == NULL)
33 {
34 ConResPuts(StdOut, IDS_SELECT_NO_PARTITION);
35 return EXIT_SUCCESS;
36 }
37
38 for (i = 1; i < argc; i++)
39 {
40 if (_wcsicmp(argv[i], L"noerr") == 0)
41 {
42 /* noerr */
43 DPRINT("NOERR\n");
44 ConPuts(StdOut, L"The NOERR option is not supported yet!\n");
45 }
46 }
47
48 for (i = 1; i < argc; i++)
49 {
50 if (HasPrefix(argv[i], L"id=", &pszSuffix))
51 {
52 /* id=<Byte>|<GUID> */
53 DPRINT("ID : %s\n", pszSuffix);
54 pszId = pszSuffix;
55 }
56 else if (_wcsicmp(argv[i], L"noerr") == 0)
57 {
58 /* noerr - Already handled above */
59 }
60 else if (_wcsicmp(argv[i], L"override") == 0)
61 {
62 /* override */
63 DPRINT("OVERRIDE\n");
64 ConPuts(StdOut, L"The OVERRIDE option is not supported yet!\n");
65 }
66 else
67 {
68 ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
69 return EXIT_SUCCESS;
70 }
71 }
72
73 if (CurrentDisk->PartitionStyle == PARTITION_STYLE_GPT)
74 {
75 if (!StringToGUID(&CurrentPartition->Gpt.PartitionType, pszId))
76 {
77 ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
78 return EXIT_SUCCESS;
79 }
80
81 CurrentDisk->Dirty = TRUE;
82 UpdateGptDiskLayout(CurrentDisk, FALSE);
83 Status = WriteGptPartitions(CurrentDisk);
84 if (!NT_SUCCESS(Status))
85 {
86 ConResPuts(StdOut, IDS_SETID_FAIL);
87 return EXIT_SUCCESS;
88 }
89 }
90 else if (CurrentDisk->PartitionStyle == PARTITION_STYLE_MBR)
91 {
92 UCHAR PartitionType = 0;
93
94 length = wcslen(pszId);
95 if (length == 0)
96 {
97 ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
98 return EXIT_SUCCESS;
99 }
100
101 if (length > 2)
102 {
103 ConResPuts(StdErr, IDS_SETID_INVALID_FORMAT);
104 return EXIT_SUCCESS;
105 }
106
107 PartitionType = (UCHAR)wcstoul(pszSuffix, NULL, 16);
108 if ((PartitionType == 0) && (errno == ERANGE))
109 {
110 ConResPuts(StdErr, IDS_SETID_INVALID_FORMAT);
111 return EXIT_SUCCESS;
112 }
113
114 if (PartitionType == 0x42)
115 {
116 ConResPuts(StdErr, IDS_SETID_INVALID_TYPE);
117 return EXIT_SUCCESS;
118 }
119
120 CurrentPartition->Mbr.PartitionType = PartitionType;
121 CurrentDisk->Dirty = TRUE;
122 UpdateMbrDiskLayout(CurrentDisk);
123 Status = WriteMbrPartitions(CurrentDisk);
124 if (!NT_SUCCESS(Status))
125 {
126 ConResPuts(StdOut, IDS_SETID_FAIL);
127 return EXIT_SUCCESS;
128 }
129 }
130
131 ConResPuts(StdOut, IDS_SETID_SUCCESS);
132
133 return EXIT_SUCCESS;
134}