~ubuntu-branches/ubuntu/karmic/libsmbios/karmic

« back to all changes in this revision

Viewing changes to bin-supported/propertyTag.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jose Luis Tallon
  • Date: 2007-04-22 13:04:55 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070422130455-t40zo63zf23cip6t
Tags: 0.13.6-1
* New upstream version
  - Adds complete support for EFI (i.e. intel-based Macs)

* Fixed FTBFS with gcc4.3 (missing includes) (Closes: #418621)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 
2
 * vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=c:cindent:textwidth=0:
 
3
 *
 
4
 * Copyright (C) 2005 Dell Inc.
 
5
 *  by Michael Brown <Michael_E_Brown@dell.com>
 
6
 * Licensed under the Open Software License version 2.1
 
7
 *
 
8
 * Alternatively, you can redistribute it and/or modify
 
9
 * it under the terms of the GNU General Public License as published
 
10
 * by the Free Software Foundation; either version 2 of the License,
 
11
 * or (at your option) any later version.
 
12
 
 
13
 * This program is distributed in the hope that it will be useful, but
 
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
16
 * See the GNU General Public License for more details.
 
17
 */
 
18
 
 
19
// compat header should always be first header if including system headers
 
20
#include "smbios/compat.h"
 
21
 
 
22
#include <string>
 
23
#include <iostream>
 
24
#include <iomanip>
 
25
#include <stdlib.h>
 
26
 
 
27
#include "smbios/ISmi.h"  // only needed if you want to use fake input (memdump.dat)
 
28
#include "smbios/SystemInfo.h" // this is the main header to include to use the C interface
 
29
#include "smbios/version.h"
 
30
#include "getopts.h"
 
31
 
 
32
// always include last if included.
 
33
#include "smbios/message.h"  // not needed outside of this lib. (mainly for gettext i18n)
 
34
 
 
35
using namespace std;
 
36
 
 
37
struct options opts[] =
 
38
    {
 
39
        {
 
40
            250, "set", "Set Dell Property Tag", "s", 1
 
41
        },
 
42
        { 252, "password", "BIOS setup password", "p", 1 },
 
43
        { 249, "rawpassword", "Do not auto-convert password to scancodes", NULL, 0 },
 
44
        { 255, "version", "Display libsmbios version information", "v", 0 },
 
45
        { 0, NULL, NULL, NULL, 0 }
 
46
    };
 
47
 
 
48
int
 
49
main (int argc, char **argv)
 
50
{
 
51
    int retval = 0;
 
52
    try
 
53
    {
 
54
        int c=0;
 
55
        char *args = 0;
 
56
 
 
57
        bool setVal = false;
 
58
        string password("");
 
59
        string newTag("");
 
60
        bool rawPassword=false;
 
61
 
 
62
        while ( (c=getopts(argc, argv, opts, &args)) != 0 )
 
63
        {
 
64
            switch(c)
 
65
            {
 
66
            case 250:
 
67
                setVal = 1;
 
68
                // Property tag can be at most 80 chars (plus '\0')
 
69
                newTag = args;
 
70
                break;
 
71
            case 252:
 
72
                password = args;
 
73
                break;
 
74
            case 249:
 
75
                rawPassword = true;
 
76
                break;
 
77
            case 255:
 
78
                cout << "Libsmbios version:    " << LIBSMBIOS_RELEASE_VERSION << endl;
 
79
                exit(0);
 
80
                break;
 
81
            default:
 
82
                break;
 
83
            }
 
84
            free(args);
 
85
        }
 
86
 
 
87
        if(setVal && (!rawPassword) && (1 == SMBIOSGetSmiPasswordCoding()) && strlen(password.c_str())>0)
 
88
        {
 
89
            cerr << endl;
 
90
            cerr << "BIOS Password encoding has been detected as SCAN CODE format." << endl;
 
91
            cerr << "Automatically changing password from ASCII coding to en_US scancode format." << endl;
 
92
            cerr << "Use the --rawpassword option to disable this, for example, if you have " << endl;
 
93
            cerr << "another language keyboard, then manually convert the ASCII password to" << endl;
 
94
            cerr << "scan code format." << endl;
 
95
            cerr << endl;
 
96
 
 
97
            char *codedPass = new char[strlen(password.c_str())+1];
 
98
            memset(codedPass, 0, strlen(password.c_str())+1);
 
99
            SMBIOSMapAsciiTo_en_US_ScanCode(codedPass, password.c_str(), strlen(password.c_str()));
 
100
            password = codedPass;
 
101
            delete []codedPass;
 
102
        }
 
103
 
 
104
#define PROP_TAG_SIZE 80
 
105
        char propertyTag[PROP_TAG_SIZE + 1] =
 
106
            {
 
107
                0,
 
108
            };
 
109
        smi::getPropertyOwnershipTag(propertyTag, PROP_TAG_SIZE);
 
110
        cout << "Existing Property Ownership Tag: " << propertyTag << endl;
 
111
 
 
112
        if (setVal)
 
113
        {
 
114
            cout << "Changing Property Ownership Tag: " << newTag << endl;
 
115
            try
 
116
            {
 
117
                smi::setPropertyOwnershipTag(password, newTag.c_str(), strlen(newTag.c_str()));
 
118
                cout << "Change Successful. The changes may not take effect until reboot, depending on system type." << endl;
 
119
            }
 
120
            catch( const smi::PasswordVerificationFailed &)
 
121
            {
 
122
                cerr << endl;
 
123
                if( strlen(password.c_str())>0 )
 
124
                {
 
125
                    cerr << "Could not set tag. BIOS setup password is enabled but the password" << endl;
 
126
                    cerr << "given was not accepted by BIOS as the correct password." << endl;
 
127
                    cerr << "    -- Verify the password and try again." << endl;
 
128
                }
 
129
                else
 
130
                {
 
131
                    cerr << "Could not set tag. BIOS setup password is enabled but no password was given." << endl;
 
132
                    cerr << "    -- Try using the '--password' option to specify the BIOS setup password." << endl;
 
133
                }
 
134
                cerr << endl;
 
135
                retval = 1;
 
136
            }
 
137
            catch( const smi::SmiExecutedWithError &)
 
138
            {
 
139
                cerr << endl;
 
140
                cerr << "Could not set tag. Common problems are:" << endl;
 
141
                cerr << endl;
 
142
                cerr << "    -- Insufficient permissions to perform operation." << endl;
 
143
                cerr << "       Try running as a more privileged account." << endl;
 
144
                cerr << "          Linux  : run as 'root' user" << endl;
 
145
                cerr << "          Windows: run as 'administrator' user" << endl;
 
146
                cerr << endl;
 
147
                cerr << "    -- dcdbas device driver not loaded." << endl;
 
148
                cerr << "       Try loading the dcdbas driver" << endl;
 
149
                cerr << "          Linux  : insmod dcdbas" << endl;
 
150
                cerr << "          Windows: dcdbas driver not yet available." << endl;
 
151
                cerr << endl;
 
152
                retval = 1;
 
153
            }
 
154
        }
 
155
    }
 
156
    catch( const smi::UnsupportedSmi &e )
 
157
    {
 
158
        cerr << endl;
 
159
        cerr << "An Error occurred. The Error message is: " << endl;
 
160
        cerr << "    " << e.what() << endl;
 
161
        cerr << endl;
 
162
        cerr << "It appears that this system does not support Property Ownership Tags." << endl;
 
163
        cerr << endl;
 
164
 
 
165
        retval = 1;
 
166
    }
 
167
    catch( const exception &e )
 
168
    {
 
169
        cerr << endl;
 
170
        cerr << "An Error occurred. The Error message is: " << endl;
 
171
        cerr << "    " << e.what() << endl;
 
172
        cerr << endl;
 
173
        cerr << "Problem reading or writing tag. Common problems are:" << endl;
 
174
        cerr << endl;
 
175
        cerr << "    -- Insufficient permissions to perform operation." << endl;
 
176
        cerr << "       Try running as a more privileged account." << endl;
 
177
        cerr << "          Linux  : run as 'root' user" << endl;
 
178
        cerr << "          Windows: run as 'administrator' user" << endl;
 
179
        cerr << endl;
 
180
        cerr << "    -- dcdbas device driver not loaded." << endl;
 
181
        cerr << "       Try loading the dcdbas driver" << endl;
 
182
        cerr << "          Linux  : insmod dcdbas" << endl;
 
183
        cerr << "          Windows: dcdbas driver not yet available." << endl;
 
184
        cerr << endl;
 
185
 
 
186
        retval = 1;
 
187
    }
 
188
    catch ( ... )
 
189
    {
 
190
        cerr << endl;
 
191
        cerr << "An Unknown Error occurred. Aborting." << endl;
 
192
        cerr << endl;
 
193
        retval = 2;
 
194
    }
 
195
 
 
196
    return retval;
 
197
}