2 // Class to manage partition attribute codes. These are binary bit fields,
3 // of which only four are currently (2/2011) documented on Wikipedia, and
4 // two others found from other sources.
6 /* This program is copyright (c) 2009-2011 by Roderick W. Smith. It is distributed
7 under the terms of the GNU GPL version 2, as detailed in the COPYING file. */
9 #define __STDC_LIMIT_MACROS
10 #define __STDC_CONSTANT_MACROS
17 #include "attributes.h"
22 string Attributes::atNames[NUM_ATR];
23 int Attributes::numAttrs = 0;
24 //Attributes::staticInit Attributes::staticInitializer;
26 // Default constructor
27 Attributes::Attributes(void) {
34 // Alternate constructor
35 Attributes::Attributes(const uint64_t a) {
40 } // alternate constructor
43 Attributes::~Attributes(void) {
45 } // Attributes destructor
47 void Attributes::Setup(void) {
50 // Most bits are undefined, so start by giving them an
52 for (int i = 0; i < NUM_ATR; i++) {
54 temp << "Undefined bit #" << i;
55 Attributes::atNames[i] = temp.str();
58 // Now reset those names that are defined....
59 atNames[0] = "system partition"; // required for computer to operate
60 atNames[1] = "hide from EFI";
61 atNames[2] = "legacy BIOS bootable";
62 atNames[60] = "read-only";
63 atNames[62] = "hidden";
64 atNames[63] = "do not automount";
65 } // Attributes::Setup()
67 // Display current attributes to user
68 void Attributes::DisplayAttributes(void) {
72 cout << "Attribute value is ";
73 cout.setf(ios::uppercase);
76 cout << hex << attributes << dec << ". Set fields are:\n";
77 for (i = 0; i < NUM_ATR; i++) {
78 if ((UINT64_C(1) << i) & attributes) {
79 cout << i << " (" << GetAttributeName(i) << ")" << "\n";
85 cout << " No fields set\n";
87 } // Attributes::DisplayAttributes()
89 // Display attributes for a partition. Note that partNum is just passed for
90 // immediate display; it's not used to access a particular partition.
91 void Attributes::ShowAttributes(const uint32_t partNum) {
95 for (bitNum = 0; bitNum < 64; bitNum++) {
96 bitset = (UINT64_C(1) << bitNum) & attributes;
98 cout << partNum+1 << ":" << bitNum << ":" << bitset
99 << " (" << GetAttributeName(bitNum) << ")" << endl;
102 } // Attributes::ShowAttributes
104 // Prompt user for attribute changes
105 void Attributes::ChangeAttributes(void) {
109 cout << "Known attributes are:\n";
115 response = GetNumber(0, NUM_ATR, 64,
116 "Toggle which attribute field (0-63, 64 or <Enter> to exit): ");
117 if (response != 64) {
118 bitValue = UINT64_C(1) << response; // Find the integer value of the bit
119 if (bitValue & attributes) { // bit is set
120 attributes &= ~bitValue; // so unset it
121 cout << "Have disabled the '" << atNames[response] << "' attribute.\n";
122 } else { // bit is not set
123 attributes |= bitValue; // so set it
124 cout << "Have enabled the '" << atNames[response] << "' attribute.\n";
127 } while (response != 64);
128 } // Attributes::ChangeAttributes()
130 // Display all defined attributes on the screen (omits undefined bits).
131 void Attributes::ListAttributes(void) {
135 for (bitNum = 0; bitNum < NUM_ATR; bitNum++) {
136 tempAttr = GetAttributeName(bitNum);
137 if (tempAttr.substr(0, 15) != "Undefined bit #" )
138 cout << bitNum << ": " << Attributes::GetAttributeName(bitNum) << "\n";
140 } // Attributes::ListAttributes
142 // multifaceted attributes access
143 // returns true upon success, false upon failure
144 bool Attributes::OperateOnAttributes(const uint32_t partNum, const string& attributeOperator, const string& attributeBits) {
146 // attribute access opcode
148 ao_or, ao_nand, ao_xor, ao_assignall, // operate on all attributes (bitmask)
149 ao_unknown, // must be after bitmask operators and before bitnum operators
150 ao_set, ao_clear, ao_toggle, ao_get // operate on a single attribute (bitnum)
151 } attribute_opcode_t; // typedef enum
153 // translate attribute operator into an attribute opcode
154 attribute_opcode_t attributeOpcode = ao_unknown; { // opcode is not known yet
155 if (attributeOperator == "or") attributeOpcode = ao_or;
156 else if (attributeOperator == "nand") attributeOpcode = ao_nand;
157 else if (attributeOperator == "xor") attributeOpcode = ao_xor;
158 else if (attributeOperator == "=") attributeOpcode = ao_assignall;
159 else if (attributeOperator == "set") attributeOpcode = ao_set;
160 else if (attributeOperator == "clear") attributeOpcode = ao_clear;
161 else if (attributeOperator == "toggle") attributeOpcode = ao_toggle;
162 else if (attributeOperator == "get") attributeOpcode = ao_get;
164 cerr << "Unknown attributes operator: " << attributeOperator << endl;
169 // get bit mask if operating on entire attribute set
170 uint64_t attributeBitMask; { if (attributeOpcode < ao_unknown) {
171 if (1 != sscanf (attributeBits.c_str(), "%qx", (long long unsigned int*) &attributeBitMask)) {
172 cerr << "Could not convert hex attribute mask" << endl;
175 }} // attributeBitMask, if
177 // get bit number and calculate bit mask if operating on a single attribute
178 int bitNum; { if (attributeOpcode > ao_unknown) {
179 if (1 != sscanf (attributeBits.c_str(), "%d", &bitNum)) {
180 cerr << "Could not convert bit number" << endl;
183 const uint64_t one = 1;
184 attributeBitMask = one << bitNum;
187 switch (attributeOpcode) {
188 // assign all attributes at once
189 case ao_assignall: attributes = attributeBitMask; break;
191 // set individual attribute(s)
193 case ao_or: attributes |= attributeBitMask; break;
195 // clear individual attribute(s)
197 case ao_nand: attributes &= ~attributeBitMask; break;
199 // toggle individual attribute(s)
201 case ao_xor: attributes ^= attributeBitMask; break;
203 // display a single attribute
205 cout << partNum+1 << ":" << bitNum << ":"
206 << bool (attributeBitMask & attributes) << endl;
210 default: break; // will never get here
214 } // Attributes::OperateOnAttributes()
216 /*******************************
218 * Non-class support functions *
220 *******************************/
222 // Display attributes
223 ostream & operator<<(ostream & os, const Attributes & data) {
224 os << data.GetAttributes();