srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1 | // attributes.cc |
| 2 | // Class to manage partition attribute codes. These are binary bit fields, |
| 3 | // of which only three are currently (2/2009) documented on Wikipedia. |
| 4 | |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 5 | /* This program is copyright (c) 2009 by Roderick W. Smith. It is distributed |
| 6 | under the terms of the GNU GPL version 2, as detailed in the COPYING file. */ |
| 7 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 8 | #define __STDC_LIMIT_MACROS |
| 9 | #define __STDC_CONSTANT_MACROS |
| 10 | |
| 11 | #include <string.h> |
| 12 | #include <stdint.h> |
| 13 | #include <stdio.h> |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 14 | #include <iostream> |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 15 | #include <sstream> |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 16 | #include "attributes.h" |
| 17 | |
| 18 | using namespace std; |
| 19 | |
| 20 | // Constructor. Its main task is to initialize the attribute name |
| 21 | // data. |
| 22 | Attributes::Attributes(void) { |
| 23 | int i; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 24 | ostringstream temp; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 25 | |
| 26 | // Most bits are undefined, so start by giving them an |
| 27 | // appropriate name |
| 28 | for (i = 1; i < NUM_ATR; i++) { |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 29 | temp << "Undefined bit #" << i; |
| 30 | atNames[i] = temp.str(); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 31 | } // for |
| 32 | |
| 33 | // Now reset those names that are defined.... |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 34 | atNames[0] = "system partition"; // required for computer to operate |
srs5694 | 8a4ddfc | 2010-03-21 19:05:49 -0400 | [diff] [blame] | 35 | atNames[1] = "hide from EFI"; |
| 36 | atNames[2] = "legacy BIOS bootable"; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 37 | atNames[60] = "read-only"; |
| 38 | atNames[62] = "hidden"; |
| 39 | atNames[63] = "do not automount"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 40 | } // Attributes constructor |
| 41 | |
| 42 | // Destructor. |
| 43 | Attributes::~Attributes(void) { |
| 44 | } // Attributes destructor |
| 45 | |
| 46 | // Display current attributes to user |
| 47 | void Attributes::DisplayAttributes(void) { |
| 48 | int i; |
| 49 | |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 50 | cout << "Attribute value is "; |
| 51 | cout.setf(ios::uppercase); |
| 52 | cout.fill('0'); |
| 53 | cout.width(16); |
| 54 | cout << hex << attributes << dec << ". Set fields are:\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 55 | for (i = 0; i < NUM_ATR; i++) { |
| 56 | if (((attributes >> i) % 2) == 1) { // bit is set |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 57 | if (atNames[NUM_ATR - i - 1].substr(0, 9) != "Undefined") |
| 58 | cout << atNames[NUM_ATR - i - 1] << "\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 59 | } // if |
| 60 | } // for |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 61 | cout.fill(' '); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 62 | } // Attributes::DisplayAttributes() |
| 63 | |
| 64 | // Prompt user for attribute changes |
| 65 | void Attributes::ChangeAttributes(void) { |
| 66 | int response, i; |
| 67 | uint64_t bitValue; |
| 68 | |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 69 | cout << "Known attributes are:\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 70 | for (i = 0; i < NUM_ATR; i++) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 71 | if (atNames[i].substr(0, 9) != "Undefined") |
| 72 | cout << i << " - " << atNames[i] << "\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 73 | } // for |
| 74 | |
| 75 | do { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 76 | response = GetNumber(0, 64, -1, (string) "Toggle which attribute field (0-63, 64 to exit): "); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 77 | if (response != 64) { |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 78 | bitValue = PowerOf2(uint32_t (NUM_ATR - response - 1)); // Find the integer value of the bit |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 79 | if ((bitValue & attributes) == bitValue) { // bit is set |
| 80 | attributes -= bitValue; // so unset it |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 81 | cout << "Have disabled the '" << atNames[response] << "' attribute.\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 82 | } else { // bit is not set |
| 83 | attributes += bitValue; // so set it |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 84 | cout << "Have enabled the '" << atNames[response] << "' attribute.\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 85 | } // if/else |
| 86 | } // if |
| 87 | } while (response != 64); |
| 88 | } // Attributes::ChangeAttributes() |
| 89 | |