summaryrefslogblamecommitdiffstats
path: root/private/ntos/fw/alpha/jnsetcom.c
blob: 3eefa31d064f7c6f72c0842daf510fea52501229 (plain) (tree)
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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862





























































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                     
/*++

Copyright (c) 1991 Microsoft Corporation
Copyright (c) 1993 Digital Equipment Corporation

Module Name:

    jnsetcom.c

Abstract:

    This program contains the common routines for the Jensen setup code,
    the firmware update program (JNUPDATE.EXE), and the FailSafe Booter.


Author:

    John DeRosa		31-July-1992.

    This module, and the entire setup program, was based on the jzsetup
    program written by David M. Robinson (davidro) of Microsoft, dated
    9-Aug-1991.

Revision History:

--*/


#include "fwp.h"
#include "jnsnvdeo.h"
#include "machdef.h"
#include "string.h"
#include "iodevice.h"
#include "jnvendor.h"
#include "fwstring.h"



#ifdef ALPHA_FW_SERDEB
//
// Variable that enables printing on the COM1 line.
//
extern BOOLEAN SerSnapshot;
#endif


#ifndef FAILSAFE_BOOTER

ULONG
JzDisplayMenu (
    IN PCHAR Choices[],
    IN ULONG NumberOfChoices,
    IN LONG DefaultChoice,
    IN ULONG CurrentLine,
    IN LONG AutobootValue,
    IN BOOLEAN ShowTheTime
    )

/*++

Routine Description:

    Displays a menu and gets a selection.

    This routine assumes that the entries in the Choices array
    do not have two adjacent blank rows, and that the first and last
    rows are not blank.

Arguments:

    Choices[]		The menu choices array

    NumberOfChoices	The 1-based number of entries in Choices[]

    DefaultChoice	The 0-based entry which should be highlighted
                        as the default choice.

    CurrentLine		The current line on the video.

    AutobootValue	If zero, do not do an autoboot countdown.
	            	If nonzero, do an autoboot countdown with this value.

    ShowTheTime		If true, the time is displayed in the upper right
                        hand corner.

Return Value:

    Returns -1 if the escape key is pressed, otherwise returns the menu item
    selected, where 0 is the first item.

--*/

{
    ULONG Index;
    UCHAR Character;
    ULONG Count;
    ULONG PreviousTime;
    ULONG RelativeTime;
    BOOLEAN Timeout;

#ifndef JNUPDATE

    //
    // Setup for autoboot
    //

    if (AutobootValue == 0) {
	Timeout = FALSE;
	AutobootValue = 1;
    } else {
	Timeout = TRUE;
	PreviousTime = FwGetRelativeTime();
    }

#else

    Timeout = FALSE;
    AutobootValue = 1;

#endif

    //
    // Display the menu
    //

    for (Index = 0; Index < NumberOfChoices ; Index++ ) {
        VenSetPosition( Index + CurrentLine, 5);
        if (Index == DefaultChoice) {
            VenSetScreenAttributes( TRUE, FALSE, TRUE);
            VenPrint(Choices[Index]);
            VenSetScreenAttributes( TRUE, FALSE, FALSE);
        } else {
            VenPrint(Choices[Index]);
        }
    }

    Character = 0;
    do {
#ifndef JNUPDATE
	if (ShowTheTime) {
	    JzShowTime(FALSE);
	}
#endif
        if (ArcGetReadStatus(ARC_CONSOLE_INPUT) == ESUCCESS) {
            ArcRead(ARC_CONSOLE_INPUT, &Character, 1, &Count);
            switch (Character) {

            case ASCII_ESC:

                VenStallExecution(10000);
                if (ArcGetReadStatus(ARC_CONSOLE_INPUT) == ESUCCESS) {
		    ArcRead(ARC_CONSOLE_INPUT, &Character, 1, &Count);
		    if (Character != '[') {
			return(-1);
		    }
		} else {
		    return(-1);
		}

		// We purposely fall through to ASCII_CSI.

            case ASCII_CSI:

                ArcRead(ARC_CONSOLE_INPUT, &Character, 1, &Count);
                VenSetPosition( DefaultChoice + CurrentLine, 5);
                VenPrint(Choices[DefaultChoice]);

                switch (Character) {

                case 'A':
                case 'D':
                    DefaultChoice--;
		    if (DefaultChoice < 0) {
			DefaultChoice = NumberOfChoices - 1;
		    }
                    if (*Choices[DefaultChoice] == 0) {
                        DefaultChoice--;
                    }
                    break;

                case 'B':
                case 'C':
                    DefaultChoice++;
		    if (DefaultChoice == NumberOfChoices) {
			DefaultChoice = 0;
		    }
                    if (*Choices[DefaultChoice] == 0) {
                        DefaultChoice++;
                    }
                    break;

                case 'H':
                    DefaultChoice = 0;
                    break;

                default:
                    break;
                }

                VenSetPosition( DefaultChoice + CurrentLine, 5);
                VenSetScreenAttributes( TRUE, FALSE, TRUE);
                VenPrint(Choices[DefaultChoice]);
                VenSetScreenAttributes( TRUE, FALSE, FALSE);
                continue;

            default:
                break;
            }
        }

	//
	// If default choice is nonzero and a timeout is active, remove
	// the timeout.
	//
			
	if ((DefaultChoice != 0) && Timeout) {
	    Timeout = FALSE;
	    VenSetPosition(NumberOfChoices + 9, 0);
	    VenPrint("\x9bK");
	}
		
#ifndef JNUPDATE

	//
	// Update the timeout value if active.
	//
		      
	if (Timeout) {
	    RelativeTime = FwGetRelativeTime();
	    if (RelativeTime != PreviousTime) {
		PreviousTime = RelativeTime;
		VenSetPosition(NumberOfChoices + 9, 62);
		VenPrint("\x9bK");
		VenPrint1("%d", AutobootValue--);
	    }
	}
#endif
		
    } while ((Character != '\n') && (Character != ASCII_CR) && (AutobootValue >= 0));

    return DefaultChoice;
}
#endif

#ifndef FAILSAFE_BOOTER
GETSTRING_ACTION
JzGetString(
    OUT PCHAR String,
    IN ULONG StringLength,
    IN PCHAR InitialString OPTIONAL,
    IN ULONG CurrentRow,
    IN ULONG CurrentColumn,
    IN BOOLEAN ShowTheTime
    )

/*++

Routine Description:

    This routine reads a string from standardin until a carriage return is
    found, StringLength is reached, or ESC is pushed.

    Semicolons are discarded.  Reason: semicolons are key characters in
    the parsing of multi-segment environment variables.  They aren not
    needed for any valid input to the firmware, so the easiest solution
    is to filter them here.

Arguments:

    String - Supplies a pointer to a location where the string is to be stored.

    StringLength - Supplies the Max Length to read.

    InitialString - Supplies an optional initial string.

    CurrentRow - Supplies the current screen row.

    CurrentColumn - Supplies the current screen column.

    ShowTheTime		If true, the time is displayed in the upper
                        right-hand corner of the screen.

Return Value:

    If the string was successfully obtained GetStringSuccess is return,
    otherwise one of the following codes is returned.

    GetStringEscape - the escape key was pressed or StringLength was reached.
    GetStringUpArrow - the up arrow key was pressed.
    GetStringDownArrow - the down arrow key was pressed.

--*/

{
    ARC_STATUS Status;
    UCHAR c;
    ULONG Count;
    PCHAR Buffer;
    PCHAR Cursor;
    PCHAR CopyPointer;
    GETSTRING_ACTION Action;

    //
    // If an initial string was supplied, update the output string.
    //

    if (ARGUMENT_PRESENT(InitialString)) {
        strcpy(String, InitialString);
        Buffer = strchr(String, 0);
    } else {
        *String = 0;
        Buffer = String;
    }

    Cursor = Buffer;

    while (TRUE) {

        //
        // Print the string.
        //

        VenSetPosition(CurrentRow, CurrentColumn);
        VenPrint(String);
        VenPrint("\x9bK");

        //
        // Print the cursor.
        //

        VenSetScreenAttributes(TRUE,FALSE,TRUE);
        VenSetPosition(CurrentRow, (Cursor - String) + CurrentColumn);

        if (Cursor >= Buffer) {
            VenPrint(" ");
        } else {
            ArcWrite(ARC_CONSOLE_OUTPUT,Cursor,1,&Count);
        }
        VenSetScreenAttributes(TRUE,FALSE,FALSE);

#ifndef JNUPDATE
        while (ShowTheTime &&
	       ArcGetReadStatus(ARC_CONSOLE_INPUT) != ESUCCESS) {
            JzShowTime(FALSE);
        }
#endif
        Status = ArcRead(ARC_CONSOLE_INPUT,&c,1,&Count);

        if (Status != ESUCCESS) {
            Action = GetStringEscape;
            goto EndGetString;
        }

        if (Buffer-String == StringLength) {
            Action = GetStringEscape;
            goto EndGetString;
        }

        switch (c) {

        case ASCII_ESC:

            //
            // If there is another character available, look to see if
            // this a control sequence, and fall through to ASCII_CSI.
            // This is an attempt to make escape sequences from a terminal work.
            //

            VenStallExecution(10000);

            if (ArcGetReadStatus(ARC_CONSOLE_INPUT) == ESUCCESS) {
                ArcRead(ARC_CONSOLE_INPUT, &c, 1, &Count);
                if (c != '[') {
                    Action = GetStringEscape;
                    goto EndGetString;
                }
            } else {
                Action = GetStringEscape;
                goto EndGetString;
            }

        case ASCII_CSI:

            ArcRead(ARC_CONSOLE_INPUT, &c, 1, &Count);
            switch (c) {

            case 'A':
                Action = GetStringUpArrow;
                goto EndGetString;

            case 'D':
                if (Cursor != String) {
                    Cursor--;
                }
                continue;

            case 'B':
                Action = GetStringDownArrow;
                goto EndGetString;

            case 'C':
                if (Cursor != Buffer) {
                    Cursor++;
                }
                continue;

            case 'H':
                Cursor = String;
                continue;

            case 'K':
                Cursor = Buffer;
                continue;

            case 'P':
                CopyPointer = Cursor;
                while (*CopyPointer) {
                    *CopyPointer = *(CopyPointer + 1);
                    CopyPointer++;
                }
                if (Buffer != String) {
                    Buffer--;
                }
                continue;

            default:
                break;
            }
            break;

        case '\r':
        case '\n':

            Action = GetStringSuccess;
            goto EndGetString;

	    //
	    // Do not bother handling the tab character properly.
	    //

	case '\t' :

            Action = GetStringEscape;
            goto EndGetString;

        case '\b':

            if (Cursor != String) {
                Cursor--;
            }
            CopyPointer = Cursor;
            while (*CopyPointer) {
                *CopyPointer = *(CopyPointer + 1);
                CopyPointer++;
            }
            if (Buffer != String) {
                Buffer--;
            }
            break;

	    //
	    // Discard any semicolons because they will corrupt multi-
	    // segment environment variables.
	    //

	case ';':

	    break;

        default:

            //
            // Store the character.
            //

            CopyPointer = ++Buffer;
            if (CopyPointer > Cursor) {
                while (CopyPointer != Cursor) {
                    *CopyPointer = *(CopyPointer - 1);
                    CopyPointer--;
                }
            }
            *Cursor++ = c;
            break;
        }
    }

    Action = GetStringEscape;

EndGetString:

    VenSetPosition(CurrentRow, (Cursor - String) + CurrentColumn);

    if (Cursor >= Buffer) {
        VenPrint(" ");
    } else {
        ArcWrite(ARC_CONSOLE_OUTPUT,Cursor,1,&Count);
    }

    //
    // Make sure we return a null string if not successful.
    //

    if (Action != GetStringSuccess) {
        *String = 0;
    }

    return(Action);
}

#endif

#if !defined(JNUPDATE) && !defined(FAILSAFE_BOOTER)
ARC_STATUS
JzEnvironmentCheckChecksum (
    VOID
    )

/*++

Routine Description:

    This routine checks the environment area checksum.


Arguments:

    None.

Return Value:

    If the checksum is good, ESUCCESS is returned, otherwise EIO is returned.

--*/

{
    PUCHAR NvChars;
    PNV_CONFIGURATION NvConfiguration;
    ULONG Index;
    ULONG Checksum1, Checksum2;

    NvConfiguration = (PNV_CONFIGURATION)NVRAM_CONFIGURATION;

    //
    // Form checksum from NVRAM data.
    //

    Checksum1 = 0;
    NvChars = (PUCHAR)&NvConfiguration->Environment[0];

    for ( Index = 0 ;
    Index < LENGTH_OF_ENVIRONMENT;
    Index++ ) {
        Checksum1 += HalpReadNVByte( NvChars++ );
    }

    //
    // Reconstitute checksum and return error if no compare.
    //

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

    if (Checksum1 != Checksum2) {
        return EIO;
    } else {
        return ESUCCESS;
    }
}
#endif

#ifndef JNUPDATE
BOOLEAN
JzGetPathMnemonicKey(
    IN PCHAR OpenPath,
    IN PCHAR Mnemonic,
    IN PULONG Key
    )

/*++

Routine Description:

    This routine looks for the given Mnemonic in OpenPath.
    If Mnemonic is a component of the path, then it converts the key
    value to an integer wich is returned in Key.

Arguments:

    OpenPath - Pointer to a string that contains an ARC pathname.

    Mnemonic - Pointer to a string that contains a ARC Mnemonic

    Key      - Pointer to a ULONG where the Key value is stored.


Return Value:

    FALSE  if mnemonic is found in path and a valid key is converted.
    TRUE   otherwise.

--*/

{

    PCHAR Tmp;
    CHAR  Digits[4];
    ULONG i;
    CHAR  String[16];

    //
    // Construct a string of the form ")mnemonic("
    //
    String[0]=')';
    for(i=1;*Mnemonic;i++) {
        String[i] = * Mnemonic++;
    }
    String[i++]='(';
    String[i]='\0';

    if ((Tmp=strstr(OpenPath,&String[1])) == NULL) {
        return TRUE;
    }

    if (Tmp != OpenPath) {
        if ((Tmp=strstr(OpenPath,String)) == NULL) {
            return TRUE;
        }
    } else {
        i--;
    }
    //
    // skip the mnemonic and convert the value in between parentesis to integer
    //
    Tmp+=i;
    for (i=0;i<3;i++) {
        if (*Tmp == ')') {
            Digits[i] = '\0';
            break;
        }
        Digits[i] = *Tmp++;
    }
    Digits[i]='\0';
    *Key = atoi(Digits);
    return FALSE;
}

#endif // ndef JNUPDATE

VOID
FwWaitForKeypress(
    IN BOOLEAN ShowTheTime
    )

/*++

Routine Description:

    This routine waits for a keypress, then returns.

Arguments:

    ShowTheTime		TRUE = display the time.
	                FALSE = do not display the time.

Return Value:

    None.

--*/

{
    UCHAR Character;
    ULONG Count;

    VenPrint(SS_PRESS_KEY_MSG);
    while (ArcGetReadStatus(ARC_CONSOLE_INPUT) != ESUCCESS) {
#if !defined(JNUPDATE) && !defined(FAILSAFE_BOOTER)
	if (ShowTheTime) {
	    JzShowTime(FALSE);
	}
#endif
    }
    ArcRead(ARC_CONSOLE_INPUT, &Character, 1, &Count);
    VenPrint1("%c2J", ASCII_CSI);
}

#if !defined(JNUPDATE) && !defined(FAILSAFE_BOOTER)

BOOLEAN
FwGetVariableSegment (
    IN ULONG SegmentNumber,
    IN OUT PCHAR Segment
    )

/*++

Routine Description:

     This routine returns the specified segment of a volatile environment
     variable.  Segments are separated by semicolons.

Arguments:

     SegmentNumber - Supplies the number of the segment to return.

     Segment - Supplies a pointer to the name of the environment variable.
               The variable may be followed by an equal sign.
               An '=' and the segment value are appended to this name and
               returned.


Return Value:

     If one or more segments exist after the specified segment, TRUE is returned,
     otherwise FALSE is returned.

--*/

{
    ULONG Index;
    ULONG Count;
    PCHAR VariableValue;
    PCHAR TempPointer;

    //
    // Remove an equal sign if present.
    //

    if ((TempPointer = strchr(Segment, '=')) != NULL) {
        *TempPointer = 0;
    }

    //
    // Get variable, add equal sign, and advance Segment to where the value
    // is to be added.
    //

    VariableValue = FwGetVolatileEnvironmentVariable(Segment);
    strcat(Segment, "=");
    Segment = strchr(Segment, '=') + 1;

    //
    // If there was no variable, return.
    //

    if (VariableValue == NULL) {
        return(FALSE);
    }

    Index = 0;
    Count = 0;

    //
    // Search for the requested segment and copy it to the return value.
    //

    while ((TempPointer = strchr(VariableValue,';')) != NULL) {
        Count = TempPointer - VariableValue;

        if (Index == SegmentNumber) {
            strncpy(Segment, VariableValue, Count);
            Segment[Count] = 0;
            return TRUE;
        }

        VariableValue += Count + 1;
        Index++;
    }

    //
    // If there is data left, copy it to the return value.
    //

    strcpy(Segment,VariableValue);

    return(FALSE);
}

#endif

#if !defined(JNUPDATE) && !defined(FAILSAFE_BOOTER)

ARC_STATUS
FwSetVariableSegment (
    IN ULONG SegmentNumber,
    IN PCHAR VariableName,
    IN PCHAR NewSegment
    )

/*++

Routine Description:

     This routine sets the specified segment of an environment variable
     IN THE VOLATILE AREA ONLY.  Segments are separated by semicolons.

Arguments:

     SegmentNumber - Supplies the number of the segment to add.

     VariableName - Supplies a pointer to the name of the environment variable.
                    The variable may be followed by an equal sign.

     NewSegment - Supplies a pointer to the new segment.

Return Value:

     Returns ESUCCESS if the segment is set, otherwise an error code is
     returned.

--*/

{
    ARC_STATUS Status;
    PCHAR Variable;
    PCHAR NextVariable;
    PCHAR EndOfVariable;
    CHAR VariableValue[256];
    ULONG Index;
    ULONG Count;

    SetupROMPendingModified = TRUE;

    Variable = FwGetVolatileEnvironmentVariable(VariableName);
    VariableValue[0] = 0;

    if (Variable != NULL) {
        EndOfVariable = strchr(Variable, 0);
        for (Index = 0; Index < SegmentNumber ; Index++ ) {
            NextVariable = strchr(Variable, ';');
            if (NextVariable != NULL) {
                Count = NextVariable - Variable + 1;
                strncat(VariableValue, Variable, Count);
                Variable = NextVariable + 1;
            } else {
                strcat(VariableValue, Variable);
                Variable = EndOfVariable;
                strcat(VariableValue, ";");
            }
        }
    } else {
        for (Index = 0; Index < SegmentNumber ; Index++ ) {
            strcat(VariableValue, ";");
        }
    }

    strcat(VariableValue, NewSegment);

    if ((Variable != NULL) && (Variable != EndOfVariable)) {
        strcat(VariableValue, ";");
        strcat(VariableValue, Variable);
    }

    Status = FwCoreSetEnvironmentVariable(VariableName, VariableValue, FALSE);
    SetupROMPendingModified = TRUE;
    return(Status);
}

#endif