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 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 15 | #include "attributes.h" |
| 16 | |
| 17 | using namespace std; |
| 18 | |
| 19 | // Constructor. Its main task is to initialize the attribute name |
| 20 | // data. |
| 21 | Attributes::Attributes(void) { |
| 22 | int i; |
| 23 | char temp[ATR_NAME_SIZE]; |
| 24 | |
| 25 | // Most bits are undefined, so start by giving them an |
| 26 | // appropriate name |
| 27 | for (i = 1; i < NUM_ATR; i++) { |
| 28 | sprintf(temp, "Undefined bit #%d", i); |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 29 | atNames[i] = temp; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 30 | } // for |
| 31 | |
| 32 | // Now reset those names that are defined.... |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 33 | atNames[0] = "system partition"; |
| 34 | atNames[60] = "read-only"; |
| 35 | atNames[62] = "hidden"; |
| 36 | atNames[63] = "do not automount"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 37 | } // Attributes constructor |
| 38 | |
| 39 | // Destructor. |
| 40 | Attributes::~Attributes(void) { |
| 41 | } // Attributes destructor |
| 42 | |
| 43 | // Display current attributes to user |
| 44 | void Attributes::DisplayAttributes(void) { |
| 45 | int i; |
| 46 | |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 47 | cout << "Attribute value is "; |
| 48 | cout.setf(ios::uppercase); |
| 49 | cout.fill('0'); |
| 50 | cout.width(16); |
| 51 | cout << hex << attributes << dec << ". Set fields are:\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 52 | for (i = 0; i < NUM_ATR; i++) { |
| 53 | if (((attributes >> i) % 2) == 1) { // bit is set |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 54 | if (atNames[NUM_ATR - i - 1].substr(0, 9) != "Undefined") |
| 55 | cout << atNames[NUM_ATR - i - 1] << "\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 56 | } // if |
| 57 | } // for |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 58 | cout.fill(' '); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 59 | } // Attributes::DisplayAttributes() |
| 60 | |
| 61 | // Prompt user for attribute changes |
| 62 | void Attributes::ChangeAttributes(void) { |
| 63 | int response, i; |
| 64 | uint64_t bitValue; |
| 65 | |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 66 | cout << "Known attributes are:\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 67 | for (i = 0; i < NUM_ATR; i++) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 68 | if (atNames[i].substr(0, 9) != "Undefined") |
| 69 | cout << i << " - " << atNames[i] << "\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 70 | } // for |
| 71 | |
| 72 | do { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 73 | 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] | 74 | if (response != 64) { |
| 75 | bitValue = PowerOf2(NUM_ATR - response - 1); // Find the integer value of the bit |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 76 | if ((bitValue & attributes) == bitValue) { // bit is set |
| 77 | attributes -= bitValue; // so unset it |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 78 | cout << "Have disabled the '" << atNames[response] << "' attribute.\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 79 | } else { // bit is not set |
| 80 | attributes += bitValue; // so set it |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 81 | cout << "Have enabled the '" << atNames[response] << "' attribute.\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 82 | } // if/else |
| 83 | } // if |
| 84 | } while (response != 64); |
| 85 | } // Attributes::ChangeAttributes() |
| 86 | |