summaryrefslogtreecommitdiffstats
path: root/private/ntos/nthals/halr96b/mips/jxenvirv.c
blob: c78505bcf2808a3ba2d4abee3561472e06bec259 (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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
// #pragma comment(exestr, "@(#) jxenvirv.c 1.1 95/09/28 15:37:20 nec")
/*++

Copyright (c) 1991-1993  Microsoft Corporation

Module Name:

    jxenvirv.c

Abstract:

    This module implements the HAL get and set environment variable routines
    for a MIPS system.

Author:


Environment:

    Kernel mode

Revision History:

    M001    95.4.25    Y.Nakatani
            - Add Interface of NVRAM for ESM
    M002 kuriyama@oa2.kb.nec.co.jp Sun May 21 20:46:25 JST 1995
	    - Change Nvram virtual address
    M003 kisimoto@oa2.kb.nec.co.jp Sat Aug 12 19:25:54 JST 1995
            - Removed _J94C_ definitions.
            _J94C_ definition indicates that the status of
            the dump switch can acknowledge from Self-test
            register.

--*/

#include "halp.h"
#include "arccodes.h"
#include "jazznvr.h"
#include "string.h"

//
// Define local upcase macro.
//

#define UpCase(c) ((c) >= 'a' && (c) <= 'z' ? (c) - ('a' - 'A') : (c))

// M002 +++
KIRQL
HalpEsmMapNvram (
    VOID
    )

/*++

Routine Description:

    This function is called to map the ESM NVRAM into a wired TB entry.

Arguments:

    None.

Return Value:

    The previous IRQL is returned as the function value.

--*/

{

    KIRQL OldIrql;
    ENTRYLO NvramPte[2];

    //
    // Construct a pair of PTE's to map NVRAM.
    //

    NvramPte[0].X1 = 0;
    NvramPte[0].PFN = 0x80013000 >> PAGE_SHIFT;
    NvramPte[0].G = 0;
    NvramPte[0].V = 1;
    NvramPte[0].D = 1;
    NvramPte[0].C = UNCACHED_POLICY;
    NvramPte[1] = NvramPte[0];
    NvramPte[1].PFN += 1;

    //
    // Raise IRQL to the highest level, allocate a TB entry, map NVRAM
    // using the alocated entry, and return the previous IRQL.
    //

    KeRaiseIrql(HIGH_LEVEL, &OldIrql);
    KeFillFixedEntryTb((PHARDWARE_PTE)&NvramPte[0],
                       (PVOID)NVRAM_VIRTUAL_BASE,
                       HalpAllocateTbEntry());

    return OldIrql;
}
// M002 ---

KIRQL
HalpMapNvram (
    VOID
    )

/*++

Routine Description:

    This function is called to map the NVRAM into a wired TB entry.

Arguments:

    None.

Return Value:

    The previous IRQL is returned as the function value.

--*/

{

    KIRQL OldIrql;
    ENTRYLO NvramPte[2];

    //
    // Construct a pair of PTE's to map NVRAM.
    //

    NvramPte[0].X1 = 0;
    NvramPte[0].PFN = NVRAM_PHYSICAL_BASE >> PAGE_SHIFT;
    NvramPte[0].G = 0;
    NvramPte[0].V = 1;
    NvramPte[0].D = 1;
    NvramPte[0].C = UNCACHED_POLICY;
    NvramPte[1] = NvramPte[0];
    NvramPte[1].PFN += 1;

    //
    // Raise IRQL to the highest level, allocate a TB entry, map NVRAM
    // using the alocated entry, and return the previous IRQL.
    //

    KeRaiseIrql(HIGH_LEVEL, &OldIrql);
    KeFillFixedEntryTb((PHARDWARE_PTE)&NvramPte[0],
                       (PVOID)NVRAM_VIRTUAL_BASE,
                       HalpAllocateTbEntry());

    return OldIrql;
}

VOID
HalpUnmapNvram (
    IN KIRQL OldIrql
    )

/*++

Routine Description:

    This function is called to unmap the NVRAM from a wired entry in
    the TB.

Arguments:

    OldIrql - Supplies the previous IRQL value.

Return Value:

    None.

--*/

{

    //
    // Free the wired TB entry that was allocated to map NVRAM and lower
    // IRQL to its previous level.
    //

    HalpFreeTbEntry();
    KeLowerIrql(OldIrql);
    return;
}

ARC_STATUS
HalpEnvironmentCheckChecksum (
    VOID
    )

/*++

Routine Description:

    This routine checks the NVRAM environment area checksum.

    N.B. The NVRAM must be mapped before this routine is called.

Arguments:

    None.

Return Value:

    ESUCCESS is returned if the checksum matches. Otherwise, EIO is returned.

--*/

{

    ULONG Checksum1;
    ULONG Checksum2;
    PUCHAR Environment;
    ULONG Index;
    PNV_CONFIGURATION NvConfiguration;

    //
    // Compute the NVRAM environment area checksum.
    //

    NvConfiguration = (PNV_CONFIGURATION)NVRAM_VIRTUAL_BASE;
    Environment = &NvConfiguration->Environment[0];
    Checksum1 = 0;
    for (Index = 0; Index < LENGTH_OF_ENVIRONMENT; Index += 1) {
        Checksum1 += (ULONG)READ_REGISTER_UCHAR(&Environment[Index]);
    }

    //
    // Merge the checksum bytes from the NVRAM and compare to computed value.
    //

    Checksum2 = (ULONG)READ_REGISTER_UCHAR(&NvConfiguration->Checksum2[0]) |
                (ULONG)READ_REGISTER_UCHAR(&NvConfiguration->Checksum2[1]) << 8 |
                (ULONG)READ_REGISTER_UCHAR(&NvConfiguration->Checksum2[2]) << 16 |
                (ULONG)READ_REGISTER_UCHAR(&NvConfiguration->Checksum2[3]) << 24;

    //
    // If the checksum mismatches, then return an I/O error. Otherwise,
    // return a success status.
    //

    if (Checksum1 != Checksum2) {
        return EIO;

    } else {
        return ESUCCESS;
    }
}

VOID
HalpEnvironmentSetChecksum (
    VOID
    )

/*++

Routine Description:

    This routine sets the NVRAM environment area checksum.

    N.B. The NVRAM must be mapped before this routine is called.

Arguments:

    None.

Return Value:

    None.

--*/

{

    ULONG Checksum;
    PUCHAR Environment;
    ULONG Index;
    PNV_CONFIGURATION NvConfiguration;

    //
    // Compute the NVRAM environment area checksum.
    //

    NvConfiguration = (PNV_CONFIGURATION)NVRAM_VIRTUAL_BASE;
    Environment = &NvConfiguration->Environment[0];
    Checksum = 0;
    for (Index = 0; Index < LENGTH_OF_ENVIRONMENT; Index += 1) {
        Checksum += (ULONG)READ_REGISTER_UCHAR(&Environment[Index]);
    }

    //
    // Write the NVRAM environment area checksum.
    //

    WRITE_REGISTER_UCHAR(&NvConfiguration->Checksum2[0],
                         (UCHAR)(Checksum & 0xFF));

    WRITE_REGISTER_UCHAR(&NvConfiguration->Checksum2[1],
                         (UCHAR)((Checksum >> 8) & 0xFF));

    WRITE_REGISTER_UCHAR(&NvConfiguration->Checksum2[2],
                         (UCHAR)((Checksum >> 16) & 0xFF));

    WRITE_REGISTER_UCHAR(&NvConfiguration->Checksum2[3],
                         (UCHAR)(Checksum >> 24));

    return;
}

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

/*++

Routine Description:

    This routine performs a case insensitive search of the NVRAM environment
    area for the specified variable name.

    N.B. The NVRAM must be mapped before this routine is called.


Arguments:

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

Return Value:

    ESUCCESS is returned if the specified variable name is located. Otherwise,
    ENOENT is returned.

--*/

{

    PUCHAR Environment;
    ULONG Index;
    PUCHAR Name;

    //
    // If the variable name is null, then return no entry found.
    //

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

    //
    // Search the environment section of the NVRAM for a variable name match.
    //

    Environment = &((PNV_CONFIGURATION)NVRAM_VIRTUAL_BASE)->Environment[0];
    Index = 0;
    do {

        //
        // Set name to the beginning of the variable name and record the
        // current index value.
        //

        Name = Variable;
        *VariableIndex = Index;

        //
        // Search until the end of the current environment variable, the
        // end of the specified variable name, or the end of the NVRAM is
        // reached.
        //

        while ((Index < LENGTH_OF_ENVIRONMENT) &&
               (READ_REGISTER_UCHAR(&Environment[Index]) != 0) && (*Name != 0)) {
            if (READ_REGISTER_UCHAR(&Environment[Index]) != UpCase(*Name)) {
                break;
            }

            Name += 1;
            Index += 1;
        }

        //
        // Check for a match which is signified by the end of the variable
        // name and the equal separator in the current environment variable.
        //

        if ((*Name == 0) && (READ_REGISTER_UCHAR(&Environment[Index]) == '=')) {
            *ValueIndex = Index + 1;
            return ESUCCESS;
        }

        //
        // Advance to the start of the next variable.
        //

        while ((Index < LENGTH_OF_ENVIRONMENT) &&
               (READ_REGISTER_UCHAR(&Environment[Index]) != 0)) {
            Index += 1;
        }

        Index += 1;
    } while (Index < LENGTH_OF_ENVIRONMENT);

    return ENOENT;
}

ARC_STATUS
HalGetEnvironmentVariable (
    IN PCHAR Variable,
    IN USHORT Length,
    OUT PCHAR Buffer
    )

/*++

Routine Description:

    This function locates an environment variable and returns its value.

Arguments:

    Variable - Supplies a pointer to a zero terminated environment variable
        name.

    Length - Supplies the length of the value buffer in bytes.

    Buffer - Supplies a pointer to a buffer that receives the variable value.

Return Value:

    ESUCCESS is returned if the enviroment variable is located. Otherwise,
    ENOENT is returned.

--*/

{

    PUCHAR Environment;
    ULONG Index;
    KIRQL OldIrql;
    ARC_STATUS Status;
    ULONG ValueIndex;
    ULONG VariableIndex;

    //
    // Map the NVRAM into the address space of the current process.
    //

    OldIrql = HalpMapNvram();

    //
    // If the checksum does not match or the specified variable cannot
    // be located, then set the status to no entry found. Otherwise, copy
    // the respective variable value to the specified output buffer.
    //

    Environment = &((PNV_CONFIGURATION)NVRAM_VIRTUAL_BASE)->Environment[0];
    if ((HalpEnvironmentCheckChecksum() != ESUCCESS) ||
        (HalpFindEnvironmentVariable(Variable,
                                     &VariableIndex,
                                     &ValueIndex) != ESUCCESS)) {

        Status = ENOENT;

    } else {

        //
        // Copy the specified value to the output buffer.
        //

        for (Index = 0; Index < Length; Index += 1) {
            *Buffer = READ_REGISTER_UCHAR(&Environment[ValueIndex]);
            if (*Buffer == 0) {
                break;
            }

            Buffer += 1;
            ValueIndex += 1;
        }

        //
        // If the length terminated the loop, then return not enough memory.
        // Otherwise, return success.
        //

        if (Index == Length) {
            Status = ENOMEM;

        } else {
            Status = ESUCCESS;
        }
    }

    //
    // Unmap the NVRAM from the address space of the current process and
    // return the function status.
    //

    HalpUnmapNvram(OldIrql);
    return Status;
}

ARC_STATUS
HalSetEnvironmentVariable (
    IN PCHAR Variable,
    IN PCHAR Value
    )

/*++

Routine Description:

    This function creates an environment variable with the specified value.

Arguments:

    Variable - Supplies a pointer to an environment variable name.

    Value - Supplies a pointer to the environment variable value.

Return Value:

    ESUCCESS is returned if the environment variable is created. Otherwise,
    ENOMEM is returned.

--*/

{

    UCHAR Character;
    PUCHAR Environment;
    KIRQL OldIrql;
    ARC_STATUS Status;
    ULONG TopIndex;
    ULONG VariableIndex;
    ULONG VariableLength;
    ULONG ValueEnd;
    ULONG ValueIndex;
    ULONG ValueLength;

    //
    // Map the NVRAM into the address space of the current process.
    //

    OldIrql = HalpMapNvram();
    Environment = &((PNV_CONFIGURATION)NVRAM_VIRTUAL_BASE)->Environment[0];

    //
    // If the checksum does not match, then set status to an I/O error.
    //

    if (HalpEnvironmentCheckChecksum() != ESUCCESS) {
        Status = EIO;
        goto Unmap;
    }

    //
    // Determine the top of the environment area by scanning backwards until
    // the a non-null character is found or the beginning of the environment
    // area is reached.
    //

    for (TopIndex = (LENGTH_OF_ENVIRONMENT - 1); TopIndex > 0; TopIndex -= 1) {
        if (READ_REGISTER_UCHAR(&Environment[TopIndex]) != '\0') {
            break;
        }
    }

    //
    // If the environment area contains any data, then adjust the top index
    // to the first free byte.
    //

    if (TopIndex != 0) {
        TopIndex += 2;
    }

    //
    // Compute the length of the variable name and the variable value.
    //

    VariableLength = strlen(Variable) + 1;
    ValueLength = strlen(Value) + 1;

    //
    // Check to determine if the specified variable is currently defined.
    //

    if (HalpFindEnvironmentVariable(Variable,
                                    &VariableIndex,
                                    &ValueIndex) == ESUCCESS) {

        //
        // The specified variable is currently defined. Determine the end
        // of the variable value by scanning forward to the zero termination
        // byte.
        //

        ValueEnd = ValueIndex;
        while (READ_REGISTER_UCHAR(&Environment[ValueEnd]) != '\0') {
            ValueEnd += 1;
        }

        ValueEnd += 1;

        //
        // If there is enough free space for the new variable value, then
        // remove the current variable name and value from the environment
        // area, insert the new variable value at the end of the environment
        // if it is not null, and set the status to success. Otherwise, set
        // the status to no space available.
        //

        if ((ValueEnd - ValueIndex + LENGTH_OF_ENVIRONMENT - TopIndex) >= ValueLength) {
            while (ValueEnd != TopIndex) {
                Character = READ_REGISTER_UCHAR(&Environment[ValueEnd]);
                WRITE_REGISTER_UCHAR(&Environment[VariableIndex], Character);
                ValueEnd += 1;
                VariableIndex += 1;
            }

            ValueIndex = VariableIndex;
            while (ValueIndex != TopIndex) {
                WRITE_REGISTER_UCHAR(&Environment[ValueIndex], '\0');
                ValueIndex += 1;
            }

            //
            // If the new variable value is not null, then copy the variable
            // name and the variable value into the enviroment area.
            //

            if (*Value != '\0') {

                //
                // copy the variable name to the environment area.
                //

                do {
                    WRITE_REGISTER_UCHAR(&Environment[VariableIndex], UpCase(*Variable));
                    VariableIndex += 1;
                    Variable += 1;
                } while (*Variable != '\0');

                //
                // Insert separator character and copy variable value to the
                // environment area.
                //

                WRITE_REGISTER_UCHAR(&Environment[VariableIndex], '=');
                VariableIndex += 1;
                do {
                    WRITE_REGISTER_UCHAR(&Environment[VariableIndex], *Value);
                    VariableIndex += 1;
                    Value += 1;
                } while (*Value != '\0');
            }

            Status = ESUCCESS;

        } else {
            Status = ENOSPC;
        }

    } else {

        //
        // The specified variable does not currently have a value. If the
        // specified variable is null or has no value, then set the status
        // to success. Otherwise, if the free area is not large enough to
        // hold the new variable name and its value, then set the status to
        // no space available. Otherwise, insert the variable name and value
        // at the end of the environment area and set the status to success.
        //

        if ((*Variable == '\0') || (*Value == '\0')) {
            Status = ESUCCESS;

        } else if ((LENGTH_OF_ENVIRONMENT - TopIndex) <
                   (VariableLength + ValueLength)) {
            Status = ENOSPC;

        } else {

            //
            // copy the variable name to the environment area.
            //

            do {
                WRITE_REGISTER_UCHAR(&Environment[TopIndex], UpCase(*Variable));
                TopIndex += 1;
                Variable += 1;
            } while (*Variable != '\0');

            //
            // Insert separator character and copy variable value to the
            // environment area.
            //

            WRITE_REGISTER_UCHAR(&Environment[TopIndex], '=');
            TopIndex += 1;
            do {
                WRITE_REGISTER_UCHAR(&Environment[TopIndex], *Value);
                TopIndex += 1;
                Value += 1;
            } while (*Value != '\0');

            Status = ESUCCESS;
        }
    }

    //
    // Compute the new checksum and write to the environment area.
    //

    HalpEnvironmentSetChecksum();

    //
    // Unmap the NVRAM from the address space of the current process.
    //

Unmap:
    HalpUnmapNvram(OldIrql);
    return Status;
}

BOOLEAN
HalNvramWrite(
    ULONG   Offset,         // Offset Of ESM NVRAM 
    ULONG   Count,          // Write Byte Count
    PVOID   Buffer         // Pointer Of Buffer Write to NVRAM
){
    // Write into NVRAM
    return HalpNvramReadWrite(Offset,Count,Buffer,1);
}

BOOLEAN
HalNvramRead(
    ULONG   Offset,         // Offset Of ESM NVRAM
    ULONG   Count,          // Read Byte Count
    PVOID   Buffer         // Pointer Of Buffer Read From NVRAM
){
    // Read From NVRAM
    return HalpNvramReadWrite(Offset,Count,Buffer,0);
}

BOOLEAN
HalpNvramReadWrite(
    ULONG   Offset,         // Read/Write offset of ESM NVRAM
    ULONG   Count,          // Read/Write Byte Count
    PVOID   Buffer,         // read/Write  Pointer
    ULONG   Write          // Operation
){

    KIRQL OldIrql;              
    ULONG       i;
    //
    // Check is addr . So decrement 1
    //
    if( 
        Offset >=0 &&
        Count  >=0 &&
        Offset 	       <= 0x1fff &&  // M002 +++
        Offset+Count-1 <= 0x1fff     // M002 ---

    ){
                        
	if(Write){
	         OldIrql = HalpEsmMapNvram(); // M002
//             if (HalpEsmDebug == 1) // test
//                    DbgBreakPoint(); // test
	         for(i=0;i<Count;i++){  // test
               		  WRITE_REGISTER_UCHAR((PUCHAR)(NVRAM_VIRTUAL_BASE+Offset+i),((PUCHAR)Buffer)[i]); // M002 +++
                 }
		 HalpUnmapNvram(OldIrql);
        }else{
	        OldIrql = HalpEsmMapNvram(); // M002
//             if (HalpEsmDebug == 1) // test
//                    DbgBreakPoint(); // test
       		for(i=0;i<Count;i++){
               		  ((PUCHAR)Buffer)[i] =READ_REGISTER_UCHAR((PUCHAR)(NVRAM_VIRTUAL_BASE+Offset+i)); // M002

		    }
		 HalpUnmapNvram(OldIrql); // M002
        }

        return TRUE;

    }else{

	//
	// It is no ESM NVRAM Erea.
	return FALSE;
    }

}