summaryrefslogtreecommitdiffstats
path: root/private/ntos/nthals/halalpha/nvenv.c
blob: 45c4a6f70fbc45c41fc767601f80da5ca33c0b8b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#if defined(TAGGED_NVRAM)

/*++

Copyright (c) 1991  Microsoft Corporation
Copyright (c) 1992, 1993, 1994  Digital Equipment Corporation

Module Name:

    nvenv.c

Abstract:

    This module implements the ARC firmware Environment Variable functions as
    described in the Advanced Risc Computing Specification (Revision 1.00),
    section 3.3.3.11.

Author:

    David M. Robinson (davidro) 13-June-1991


Revision History:

    Dave Richards  94.11.03

        Added support for the tagged NVRAM layout/API.

    James Livingston  94.05.17

        Fix a coding error that caused NVRAM to get trashed in the attempt
        to update a nearly full environment variable space.

    Steve Jenness  93.12.20

        The firmware still requires the naked checksum set and check
        routines.  Add them back in under the AXP_FIRMWARE conditional
        until the jxenvir.c in the firmware and the environ.c files
        are rationalized.

    Steve Jenness. 93.12.17

        Reduce the reads and writes to the NVRAM part.  Some parts are
        slow to access (especially on writes).  Do NVRAM access only when
        really necessary.  Remove use of HalpCopyNVRamBuffer because it is
        too difficult to make generically fast (put more intelligence
        in higher code instead).

    Steve Brooks. 6-Oct 1993 remove all platform and device specific references

        These routines have been restructured to be platform and device
        independant. All calls access the NVRAM via the calls
        HalpReadNVRamBuffer, HalpWriteNVRamBuffer, and HalpCopyNVRamBuffer

    Jeff McLeman (DEC) 31-Jul-1992 modify for Jensen

--*/

#include "arccodes.h"
#include "halp.h"
#include "nvram.h"

//
// Routine prototypes.
//

ARC_STATUS
HalpFindEnvironmentVariable (
    IN PCHAR Variable,
    OUT PULONG VariableIndex,
    OUT PULONG ValueIndex
    );

ARC_STATUS
HalGetEnvironmentVariable (
    IN PCHAR Variable,
    IN USHORT length,
    OUT PCHAR Buffer
    );

ARC_STATUS
HalSetEnvironmentVariable (
    IN PCHAR Variable,
    IN PCHAR Value
    );

#ifdef AXP_FIRMWARE

#pragma alloc_text(DISTEXT, HalpFindEnvironmentVariable )
#pragma alloc_text(DISTEXT, HalGetEnvironmentVariable )
#pragma alloc_text(DISTEXT, HalSetEnvironmentVariable )

#endif // AXP_FIRMWARE

ARC_STATUS
HalpFindEnvironmentVariable (
    IN PCHAR Variable,
    OUT PULONG VariableIndex,
    OUT PULONG ValueIndex
    )

/*++

Routine Description:

    This routine searches (not case sensitive) the supplied NVRAM image
    for the given Variable.

Arguments:

    Variable - Supplies a zero terminated string containing an environment
               variable.
    VariableIndex - Returned byte offset into Environment of the
                    Variable if found.
    ValueIndex - Returned byte offset into Environment of the
                 value of the Variable if found.

Return Value:

    ESUCCESS - Variable found and indicies returned.
    ENOENT - Variable not found.

--*/

{
    ULONG EnvironmentOffset;
    ULONG EnvironmentLength;
    PUCHAR String;
    UCHAR Char;
    ULONG Index;

    //
    // If Variable is null, return immediately.
    //

    if (*Variable == 0) {
        return ENOENT;
    }

    //
    // Get the offset and length of the environment in NVRAM.
    //

    EnvironmentOffset = HalpGetNVRamEnvironmentOffset();
    EnvironmentLength = HalpGetNVRamEnvironmentLength();

    Index = 0;

    while (TRUE) {

        //
        // Set string to beginning of Variable.
        //

        String = Variable;
        *VariableIndex = EnvironmentOffset + Index;

        //
        // Search until the end of NVRAM.
        //

        while ( Index < EnvironmentLength ) {

            //
            // Convert to uppercase and break if mismatch.
            //

            Char = HalpGetNVRamUchar(EnvironmentOffset + Index);

            if ( Char != ((*String >= 'a') && (*String <= 'z') ?
                                (*String - 'a' + 'A') : *String) ) {
                break;
            }

            String++;
            Index++;
        }

        if ( Index == EnvironmentLength )
            return ENOENT;

        //
        // Check to see if we're at the end of the string and the variable,
        // which means a match.
        //

        Char = HalpGetNVRamUchar(EnvironmentOffset + Index);
        if ((*String == 0) && (Char == '=')) {
            *ValueIndex = EnvironmentOffset + ++Index;
            return ESUCCESS;
        }

        //
        // Move index to the start of the next variable.
        //

        do {
            Char = HalpGetNVRamUchar(EnvironmentOffset + Index++);

            if (Index >= EnvironmentLength) {
                return ENOENT;
            }

        } while (Char != 0);

    }
}

ARC_STATUS
HalGetEnvironmentVariable (
    IN PCHAR Variable,
    IN USHORT length,
    OUT PCHAR Buffer
    )

/*++

Routine Description:

    This routine searches (not case sensitive) the non-volatile ram for
    Variable, and if found returns a pointer to a zero terminated string that
    contains the value, otherwise a NULL pointer is returned.


Arguments:

    Variable - Supplies a zero terminated string containing an environment
               variable.
    Length - Supplies the length of the value buffer in bytes

    Buffer - Supplies a pointer to a buffer that will recieve the 
             environment variable.

Return Value:

    ESUCCESS - Buffer contains the zero terminated string value of Variable.
    ENOENT - The variable doesn't exist in the environment.
    ENOMEM - The variable exists, but the value is longer than Length.

--*/

{
    ULONG NvChars;
    ULONG VariableIndex;
    ULONG ValueIndex;
    ULONG Index;
    ARC_STATUS Status;

    //
    // If checksum is wrong, or the variable can't be found, return NULL.
    //

    if ((!HalpIsNVRamRegion0Valid()) ||
        (HalpFindEnvironmentVariable(Variable,
                                     &VariableIndex,
                                     &ValueIndex) != ESUCCESS)) {
        Status = ENOENT;
    } else {

        //
        // Copy value to an output string, break on zero terminator
        // or string max.
        //

        NvChars = ValueIndex;
        for ( Index = 0 ; Index < length ; Index += 1 ) {
            if ( (*Buffer++ = HalpGetNVRamUchar(NvChars++)) == 0 ) {
                break;
            }
        }

        if (Index == length) {
            Status = ENOMEM;
        } else {
            Status = ESUCCESS;
        }
    }

    return Status;
}


ARC_STATUS
HalSetEnvironmentVariable (
    IN PCHAR Variable,
    IN PCHAR Value
    )

/*++

Routine Description:

    This routine sets Variable (not case sensitive) to Value.

Arguments:

    Variable - Supplies a zero terminated string containing an environment
               variable.

    Value - Supplies a zero terminated string containing an environment
            variable value.

Return Value:

    ESUCCESS - The set completed successfully
    ENOSPC - No space in NVRAM for set operation.
    EIO - Invalid Checksum.

--*/

{
    ULONG EnvironmentOffset;
    ULONG EnvironmentLength;
    ULONG VariableIndex;
    ULONG ValueIndex;
    ULONG TopOfEnvironment;
    PCHAR String;
    ULONG NvChars;
    LONG Count;
    CHAR Char;

    //
    // If checksum is wrong, return EIO;
    //

    if (!HalpIsNVRamRegion0Valid()) {
        return EIO;
    }

    //
    // Get the offset and length of the environment in NVRAM.
    //

    EnvironmentOffset = HalpGetNVRamEnvironmentOffset();
    EnvironmentLength = HalpGetNVRamEnvironmentLength();

//
//smjfix - examine the boundary condition where the environment space
//         is exactly filled.

    //
    // Determine the top of the environment space by looking for the first
    // non-null character from the top.
    //

    TopOfEnvironment = EnvironmentOffset + EnvironmentLength - 1;

    do {
        Char = HalpGetNVRamUchar(TopOfEnvironment);

    } while ( Char == 0 && (--TopOfEnvironment > EnvironmentOffset) );

    //
    // Adjust TopOfEnvironment to the first new character, unless environment
    // space is empty, or the environment is exactly full.  In the latter
    // case, the new value MUST fit into the space taken by the old.
    //

    if (TopOfEnvironment != EnvironmentOffset &&
        TopOfEnvironment < EnvironmentOffset + EnvironmentLength - 2) {
        TopOfEnvironment += 2;
    }

    //
    // Handle the case where the content of the NVRAM has been corrupted
    // such that the last character in the environment is non-zero.
    //

    Count = (EnvironmentOffset + EnvironmentLength - 1) - TopOfEnvironment;
    if (Count < 0) {
        return ENOSPC;
    }


    //
    // Check to see if the variable already has a value.
    //

    if (HalpFindEnvironmentVariable(Variable, &VariableIndex, &ValueIndex) 
        == ESUCCESS) {
        ULONG SizeOfValue = strlen(Value);

        if (SizeOfValue == HalpGetNVRamStringLength(ValueIndex)) {

            //
            // Overwrite the current variable in place.
            //

            HalpMoveMemoryToNVRam(ValueIndex, Value, SizeOfValue);

            //
            // Suppress the append of the variable to the end of the
            // environment variable data.
            //

            *Value = 0;

        } else {

            //
            // Count free space, starting with the free area at the top and
            // adding the old variable value.
            //

            for ( NvChars = ValueIndex;
                  NvChars <= TopOfEnvironment;
                  NvChars++ ) {

                  Char = HalpGetNVRamUchar(NvChars);
                  if ( Char == 0 )
                    break;
                      Count++;
            }

            //
            // Determine if free area is large enough to handle new value, if
            // not return error.
            //

            for ( String = Value ; *String != 0 ; String++ ) {
                if (Count-- == 0) {
                    return ENOSPC;
                }
            }

            //
            // Move ValueIndex to the end of the value and compress strings.
            //

            do {
                Char = HalpGetNVRamUchar(ValueIndex++);
            } while( Char != 0 );

            Count = TopOfEnvironment - ValueIndex;

            HalpMoveNVRamToNVRam(VariableIndex, ValueIndex, Count);

            //
            // Adjust new top of environment.
            //

            TopOfEnvironment = VariableIndex+Count;

            //
            // Zero to the end.
            //

            Count = EnvironmentOffset + EnvironmentLength - TopOfEnvironment;
            Char = 0;
            while ( Count -- ) {
                HalpSetNVRamUchar(TopOfEnvironment + Count, Char);
            }
        }

    } else {

        //
        // Variable is new.
        //

        //
        // Determine if free area is large enough to handle new value, if not
        // return error.
        //

        //
        // From the length of free space subtract new variable's length,
        // Value's length and 2 chars, one for the '=' sign and one of the
        // null terminator.
        //

        Count -= ( strlen(Variable) + strlen(Value) + 2 );

        //
        // Check if there is space to fit the new variable.
        //

        if (Count < 0) {
            return ENOSPC;
        }

    }

    //
    // If Value is not zero, append the new variable and value.
    //

    if (*Value != 0) {

        //
        // Write new variable, converting to upper case.
        //
        while ( *Variable != 0 ) {

            Char = ((*Variable >= 'a') && (*Variable <= 'z') ?
                         (*Variable - 'a' + 'A') : *Variable);

            HalpSetNVRamUchar(TopOfEnvironment, Char);

            Variable ++;
            TopOfEnvironment ++;
        }

        //
        // Write equal sign.
        //

        Char = '=';
        HalpSetNVRamUchar(TopOfEnvironment++, Char);

        //
        // Write new value.
        //

        for ( Count = 0; Value[Count] != 0; Count ++ )
            ;

        HalpMoveMemoryToNVRam(TopOfEnvironment, Value, Count);

    }

    //
    // Write the environment variables out to NVRAM and checksum it.
    //

    if (HalpSynchronizeNVRamRegion0(TRUE)) {
        return ESUCCESS;
    } else {
        return EIO;
    }
}

#endif // TAGGED_NVRAM