blob: 527dc8795f56cef545c999c13471c4ccf5bf052d [file] [log] [blame]
srs5694e7b4ff92009-08-18 13:16:10 -04001// 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
srs5694221e0872009-08-29 15:00:31 -04005/* 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
srs5694e7b4ff92009-08-18 13:16:10 -04008#define __STDC_LIMIT_MACROS
9#define __STDC_CONSTANT_MACROS
10
11#include <string.h>
12#include <stdint.h>
13#include <stdio.h>
srs5694fed16d02010-01-27 23:03:40 -050014#include <iostream>
srs569408bb0da2010-02-19 17:19:55 -050015#include <sstream>
srs5694e7b4ff92009-08-18 13:16:10 -040016#include "attributes.h"
17
18using namespace std;
19
20// Constructor. Its main task is to initialize the attribute name
21// data.
22Attributes::Attributes(void) {
23 int i;
srs569408bb0da2010-02-19 17:19:55 -050024 ostringstream temp;
srs5694e7b4ff92009-08-18 13:16:10 -040025
26 // Most bits are undefined, so start by giving them an
27 // appropriate name
28 for (i = 1; i < NUM_ATR; i++) {
srs569408bb0da2010-02-19 17:19:55 -050029 temp << "Undefined bit #" << i;
30 atNames[i] = temp.str();
srs5694e7b4ff92009-08-18 13:16:10 -040031 } // for
32
33 // Now reset those names that are defined....
srs56946699b012010-02-04 00:55:30 -050034 atNames[0] = "system partition"; // required for computer to operate
srs56948a4ddfc2010-03-21 19:05:49 -040035 atNames[1] = "hide from EFI";
36 atNames[2] = "legacy BIOS bootable";
srs5694fed16d02010-01-27 23:03:40 -050037 atNames[60] = "read-only";
38 atNames[62] = "hidden";
39 atNames[63] = "do not automount";
srs5694e7b4ff92009-08-18 13:16:10 -040040} // Attributes constructor
41
42// Destructor.
43Attributes::~Attributes(void) {
44} // Attributes destructor
45
46// Display current attributes to user
47void Attributes::DisplayAttributes(void) {
48 int i;
49
srs5694fed16d02010-01-27 23:03:40 -050050 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";
srs5694e7b4ff92009-08-18 13:16:10 -040055 for (i = 0; i < NUM_ATR; i++) {
56 if (((attributes >> i) % 2) == 1) { // bit is set
srs5694fed16d02010-01-27 23:03:40 -050057 if (atNames[NUM_ATR - i - 1].substr(0, 9) != "Undefined")
58 cout << atNames[NUM_ATR - i - 1] << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -040059 } // if
60 } // for
srs5694fed16d02010-01-27 23:03:40 -050061 cout.fill(' ');
srs5694e7b4ff92009-08-18 13:16:10 -040062} // Attributes::DisplayAttributes()
63
64// Prompt user for attribute changes
65void Attributes::ChangeAttributes(void) {
66 int response, i;
67 uint64_t bitValue;
68
srs5694fed16d02010-01-27 23:03:40 -050069 cout << "Known attributes are:\n";
srs5694e7b4ff92009-08-18 13:16:10 -040070 for (i = 0; i < NUM_ATR; i++) {
srs5694fed16d02010-01-27 23:03:40 -050071 if (atNames[i].substr(0, 9) != "Undefined")
72 cout << i << " - " << atNames[i] << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -040073 } // for
74
75 do {
srs5694fed16d02010-01-27 23:03:40 -050076 response = GetNumber(0, 64, -1, (string) "Toggle which attribute field (0-63, 64 to exit): ");
srs5694e7b4ff92009-08-18 13:16:10 -040077 if (response != 64) {
srs5694a8582cf2010-03-19 14:21:59 -040078 bitValue = PowerOf2(uint32_t (NUM_ATR - response - 1)); // Find the integer value of the bit
srs5694e7b4ff92009-08-18 13:16:10 -040079 if ((bitValue & attributes) == bitValue) { // bit is set
80 attributes -= bitValue; // so unset it
srs5694fed16d02010-01-27 23:03:40 -050081 cout << "Have disabled the '" << atNames[response] << "' attribute.\n";
srs5694e7b4ff92009-08-18 13:16:10 -040082 } else { // bit is not set
83 attributes += bitValue; // so set it
srs5694fed16d02010-01-27 23:03:40 -050084 cout << "Have enabled the '" << atNames[response] << "' attribute.\n";
srs5694e7b4ff92009-08-18 13:16:10 -040085 } // if/else
86 } // if
87 } while (response != 64);
88} // Attributes::ChangeAttributes()
89