2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/config/powerpc/cmhardwr.c
5 * PURPOSE: Configuration Manager - Hardware-Specific Code
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
9 /* INCLUDES ******************************************************************/
15 /* GLOBALS *******************************************************************/
17 PCHAR CmpID1
= "PowerPC %u";
18 PCHAR CmpID2
= "No Data";
19 PCHAR CmpBiosStrings
[] =
24 "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9",
25 "v 0", "v 1", "v 2", "v 3", "v 4", "v 5", "v 6", "v 7", "v 8", "v 9",
29 PCHAR CmpBiosBegin
, CmpBiosSearchStart
, CmpBiosSearchEnd
;
31 /* FUNCTIONS *****************************************************************/
35 CmpGetBiosDate(IN PCHAR BiosStart
,
40 CHAR LastDate
[11] = {0}, CurrentDate
[11];
43 /* Skip the signature and the magic, and loop the BIOS ROM */
45 pp
= BiosStart
+ BiosLength
- 5;
48 /* Check for xx/yy/zz which we assume to be a date */
57 /* Copy the string proper */
58 RtlMoveMemory(&CurrentDate
[5], p
- 2, 5);
60 /* Add a 0 if the month only has one digit */
61 if (!isdigit(CurrentDate
[5])) CurrentDate
[5] = '0';
63 /* Now copy the year */
64 CurrentDate
[2] = p
[4];
65 CurrentDate
[3] = p
[5];
66 CurrentDate
[4] = CurrentDate
[7] = CurrentDate
[10] = ANSI_NULL
;
68 /* If the date comes from the BIOS, check if it's a 4-digit year */
72 ((RtlEqualMemory(&p
[4], "19", 2)) ||
73 (RtlEqualMemory(&p
[4], "20", 2))))
75 /* Copy the year proper */
76 CurrentDate
[0] = p
[4];
77 CurrentDate
[1] = p
[5];
78 CurrentDate
[2] = p
[6];
79 CurrentDate
[3] = p
[7];
83 /* Otherwise, we'll just assume anything under 80 is 2000 */
84 if (strtoul(&CurrentDate
[2], NULL
, 10) < 80)
86 /* Hopefully your BIOS wasn't made in 1979 */
92 /* Anything over 80, was probably made in the 1900s... */
98 /* Add slashes where we previously had NULLs */
99 CurrentDate
[4] = CurrentDate
[7] = '/';
101 /* Check which date is newer */
102 if (memcmp(LastDate
, CurrentDate
, 10) < 0)
104 /* Found a newer date, select it */
105 RtlMoveMemory(LastDate
, CurrentDate
, 10);
113 /* Make sure we found a date */
116 /* Copy the year at the pp, and keep only the last two digits */
117 RtlMoveMemory(BiosDate
, &LastDate
[5], 5);
119 BiosDate
[6] = LastDate
[2];
120 BiosDate
[7] = LastDate
[3];
121 BiosDate
[8] = ANSI_NULL
;
125 /* No date found, return empty string */
126 BiosDate
[0] = ANSI_NULL
;
132 CmpGetBiosVersion(IN PCHAR BiosStart
,
134 IN PCHAR BiosVersion
)
140 /* Check if we were given intitial data for the search */
143 /* Save it for later use */
144 CmpBiosBegin
= BiosStart
;
145 CmpBiosSearchStart
= BiosStart
+ 1;
146 CmpBiosSearchEnd
= BiosStart
+ BiosLength
- 2;
149 /* Now loop the BIOS area */
152 /* Start an initial search looking for numbers and periods */
154 while (CmpBiosSearchStart
<= CmpBiosSearchEnd
)
156 /* Check if we have an "x.y" version string */
157 if ((*CmpBiosSearchStart
== '.') &&
158 (*(CmpBiosSearchStart
+ 1) >= '0') &&
159 (*(CmpBiosSearchStart
+ 1) <= '9') &&
160 (*(CmpBiosSearchStart
- 1) >= '0') &&
161 (*(CmpBiosSearchStart
- 1) <= '9'))
163 /* Start looking in this area for the actual BIOS Version */
164 pp
= CmpBiosSearchStart
;
170 CmpBiosSearchStart
++;
174 /* Break out if we're went past the BIOS area */
175 if (CmpBiosSearchStart
> CmpBiosSearchEnd
) return FALSE
;
177 /* Move to the next 2 bytes */
178 CmpBiosSearchStart
+= 2;
180 /* Null-terminate our scratch buffer and start the string here */
181 Buffer
[127] = ANSI_NULL
;
184 /* Go back one character since we're doing this backwards */
187 /* Loop the identifier we found as long as it's valid */
189 while ((i
++ < 127) &&
190 (pp
>= CmpBiosBegin
) &&
194 /* Copy the character */
198 /* Go past the last character since we went backwards */
201 /* Loop the strings we recognize */
202 for (i
= 0; CmpBiosStrings
[i
]; i
++)
204 /* Check if a match was found */
205 if (strstr(p
, CmpBiosStrings
[i
])) goto Match
;
210 /* Skip until we find a space */
211 for (; *pp
== ' '; pp
++);
213 /* Loop the final string */
217 /* Copy the character into the final string */
218 BiosVersion
[i
] = *pp
++;
219 } while ((++i
< 127) &&
220 (pp
<= (CmpBiosSearchEnd
+ 1)) &&
224 /* Null-terminate the version string */
225 BiosVersion
[i
] = ANSI_NULL
;
231 CmpInitializeMachineDependentConfiguration(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
233 UNICODE_STRING KeyName
, ValueName
, Data
, SectionName
;
234 OBJECT_ATTRIBUTES ObjectAttributes
;
235 ULONG HavePae
, CacheSize
, ViewSize
, Length
, TotalLength
= 0, i
, Disposition
;
237 HANDLE KeyHandle
, BiosHandle
, SystemHandle
, FpuHandle
, SectionHandle
;
238 CONFIGURATION_COMPONENT_DATA ConfigData
;
240 ULONG ExtendedId
= 0; //, Dummy;
242 USHORT IndexTable
[MaximumType
+ 1] = {0};
243 ANSI_STRING TempString
;
244 PCHAR PartialString
= NULL
, BiosVersion
;
246 PVOID BaseAddress
= NULL
;
247 LARGE_INTEGER ViewBase
= {{0, 0}};
248 ULONG_PTR VideoRomBase
;
249 PCHAR CurrentVersion
;
250 extern UNICODE_STRING KeRosProcessorName
, KeRosBiosDate
, KeRosBiosVersion
;
251 extern UNICODE_STRING KeRosVideoBiosDate
, KeRosVideoBiosVersion
;
253 /* Open the SMSS Memory Management key */
254 RtlInitUnicodeString(&KeyName
,
255 L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\"
256 L
"Control\\Session Manager\\Memory Management");
257 InitializeObjectAttributes(&ObjectAttributes
,
259 OBJ_CASE_INSENSITIVE
,
262 Status
= NtOpenKey(&KeyHandle
, KEY_READ
| KEY_WRITE
, &ObjectAttributes
);
263 if (NT_SUCCESS(Status
))
265 /* Detect if PAE is enabled */
266 HavePae
= SharedUserData
->ProcessorFeatures
[PF_PAE_ENABLED
];
269 RtlInitUnicodeString(&ValueName
, L
"PhysicalAddressExtension");
270 NtSetValueKey(KeyHandle
,
281 /* Open the hardware description key */
282 RtlInitUnicodeString(&KeyName
,
283 L
"\\Registry\\Machine\\Hardware\\Description\\System");
284 InitializeObjectAttributes(&ObjectAttributes
,
286 OBJ_CASE_INSENSITIVE
,
289 Status
= NtOpenKey(&SystemHandle
, KEY_READ
| KEY_WRITE
, &ObjectAttributes
);
290 if (!NT_SUCCESS(Status
))
293 /* Create the BIOS Information key */
294 RtlInitUnicodeString(&KeyName
,
295 L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\"
296 L
"Control\\BIOSINFO");
297 InitializeObjectAttributes(&ObjectAttributes
,
299 OBJ_CASE_INSENSITIVE
,
302 Status
= NtCreateKey(&BiosHandle
,
307 REG_OPTION_NON_VOLATILE
,
309 if (ExpInTextModeSetup
)
311 if (!NT_SUCCESS(Status
))
314 else if (!NT_SUCCESS(Status
))
316 NtClose(SystemHandle
);
320 /* Create the CPU Key, and check if it already existed */
321 RtlInitUnicodeString(&KeyName
, L
"CentralProcessor");
322 InitializeObjectAttributes(&ObjectAttributes
,
324 OBJ_CASE_INSENSITIVE
,
327 Status
= NtCreateKey(&KeyHandle
,
328 KEY_READ
| KEY_WRITE
,
336 /* The key shouldn't already exist */
337 if (Disposition
== REG_CREATED_NEW_KEY
)
339 /* Allocate the configuration data for cmconfig.c */
340 CmpConfigurationData
= ExAllocatePoolWithTag(PagedPool
,
341 CmpConfigurationAreaSize
,
343 if (!CmpConfigurationData
)
345 // FIXME: Cleanup stuff!!
346 return STATUS_INSUFFICIENT_RESOURCES
;
350 for (i
= 0; i
< KeNumberProcessors
; i
++)
353 Prcb
= KiProcessorBlock
[i
];
355 /* Setup the Configuration Entry for the Processor */
356 RtlZeroMemory(&ConfigData
, sizeof(ConfigData
));
357 ConfigData
.ComponentEntry
.Class
= ProcessorClass
;
358 ConfigData
.ComponentEntry
.Type
= CentralProcessor
;
359 ConfigData
.ComponentEntry
.Key
= i
;
360 ConfigData
.ComponentEntry
.AffinityMask
= AFFINITY_MASK(i
);
361 ConfigData
.ComponentEntry
.Identifier
= Buffer
;
363 /* Check if the CPU doesn't support CPUID */
366 /* Build ID1-style string for older CPUs */
370 (Prcb
->CpuStep
>> 8) + 'A',
371 Prcb
->CpuStep
& 0xff);
375 /* Build ID2-style string for newer CPUs */
379 (Prcb
->CpuStep
>> 8),
380 Prcb
->CpuStep
& 0xff);
383 /* Save the ID string length now that we've created it */
384 ConfigData
.ComponentEntry
.IdentifierLength
= strlen(Buffer
) + 1;
386 /* Initialize the registry configuration node for it */
387 Status
= CmpInitializeRegistryNode(&ConfigData
,
390 InterfaceTypeUndefined
,
393 if (!NT_SUCCESS(Status
))
396 NtClose(SystemHandle
);
401 /* Setup the Configuration Entry for the FPU */
402 RtlZeroMemory(&ConfigData
, sizeof(ConfigData
));
403 ConfigData
.ComponentEntry
.Class
= ProcessorClass
;
404 ConfigData
.ComponentEntry
.Type
= FloatingPointProcessor
;
405 ConfigData
.ComponentEntry
.Key
= i
;
406 ConfigData
.ComponentEntry
.AffinityMask
= AFFINITY_MASK(i
);
407 ConfigData
.ComponentEntry
.Identifier
= Buffer
;
409 /* For 386 cpus, the CPU pp is the identifier */
410 if (Prcb
->CpuType
== 3) strcpy(Buffer
, "80387");
412 /* Save the ID string length now that we've created it */
413 ConfigData
.ComponentEntry
.IdentifierLength
= strlen(Buffer
) + 1;
415 /* Initialize the registry configuration node for it */
416 Status
= CmpInitializeRegistryNode(&ConfigData
,
419 InterfaceTypeUndefined
,
422 if (!NT_SUCCESS(Status
))
424 /* We failed, close all the opened handles and return */
427 NtClose(SystemHandle
);
431 /* Close this new handle */
434 /* Stay on this CPU only */
435 KeSetSystemAffinityThread(Prcb
->SetMember
);
438 /* Uh oh, no CPUID! */
442 /* Check if we have extended CPUID that supports name ID */
443 //Ki386Cpuid(0x80000000, &ExtendedId, &Dummy, &Dummy, &Dummy);
444 if (ExtendedId
>= 0x80000004)
446 /* Do all the CPUIDs required to get the full name */
447 PartialString
= CpuString
;
448 for (ExtendedId
= 2; ExtendedId
<= 4; ExtendedId
++)
451 /* Do the CPUID and save the name string */
452 Ki386Cpuid(0x80000000 | ExtendedId
,
453 (PULONG
)PartialString
,
454 (PULONG
)PartialString
+ 1,
455 (PULONG
)PartialString
+ 2,
456 (PULONG
)PartialString
+ 3);
459 /* Go to the next name string */
463 /* Null-terminate it */
464 CpuString
[47] = ANSI_NULL
;
468 /* Get the cache size while we're still localized */
469 CacheSize
= 0; //((PKIPCR)KeGetPcr())->SecondLevelCacheSize;
471 /* Go back to user affinity */
472 KeRevertToUserAffinityThread();
474 /* Check if we have a CPU Name */
477 /* Convert it to Unicode */
478 RtlInitAnsiString(&TempString
, CpuString
);
479 RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
481 /* Add it to the registry */
482 RtlInitUnicodeString(&ValueName
, L
"ProcessorNameString");
483 Status
= NtSetValueKey(KeyHandle
,
488 Data
.Length
+ sizeof(UNICODE_NULL
));
490 /* ROS: Save a copy for bugzilla reporting */
491 RtlCreateUnicodeString(&KeRosProcessorName
, Data
.Buffer
);
493 /* Free the temporary buffer */
494 RtlFreeUnicodeString(&Data
);
497 /* Check if we had a Vendor ID */
498 if (Prcb
->VendorString
)
500 /* Convert it to Unicode */
501 RtlInitAnsiString(&TempString
, Prcb
->VendorString
);
502 RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
504 /* Add it to the registry */
505 RtlInitUnicodeString(&ValueName
, L
"VendorIdentifier");
506 Status
= NtSetValueKey(KeyHandle
,
511 Data
.Length
+ sizeof(UNICODE_NULL
));
513 /* Free the temporary buffer */
514 RtlFreeUnicodeString(&Data
);
517 /* Check if we have features bits */
518 if (Prcb
->FeatureBits
)
520 /* Add them to the registry */
521 RtlInitUnicodeString(&ValueName
, L
"FeatureSet");
522 Status
= NtSetValueKey(KeyHandle
,
527 sizeof(Prcb
->FeatureBits
));
530 /* Check if we detected the CPU Speed */
533 /* Add it to the registry */
534 RtlInitUnicodeString(&ValueName
, L
"~MHz");
535 Status
= NtSetValueKey(KeyHandle
,
543 /* Check if we have an update signature */
544 if (Prcb
->UpdateSignature
.QuadPart
)
546 /* Add it to the registry */
547 RtlInitUnicodeString(&ValueName
, L
"Update Signature");
548 Status
= NtSetValueKey(KeyHandle
,
552 &Prcb
->UpdateSignature
,
553 sizeof(Prcb
->UpdateSignature
));
556 /* Close the processor handle */
559 /* FIXME: Detect CPU mismatches */
563 /* Free the configuration data */
564 ExFreePoolWithTag(CmpConfigurationData
, TAG_CM
);
567 /* Open physical memory */
568 RtlInitUnicodeString(&SectionName
, L
"\\Device\\PhysicalMemory");
569 InitializeObjectAttributes(&ObjectAttributes
,
571 OBJ_CASE_INSENSITIVE
,
574 Status
= ZwOpenSection(&SectionHandle
,
577 if (!NT_SUCCESS(Status
))
579 /* We failed, close all the opened handles and return */
580 // NtClose(KeyHandle);
582 NtClose(SystemHandle
);
583 /* 'Quickie' closes KeyHandle */
587 /* Map the first 1KB of memory to get the IVT */
588 ViewSize
= PAGE_SIZE
;
589 Status
= ZwMapViewOfSection(SectionHandle
,
599 if (!NT_SUCCESS(Status
))
602 VideoRomBase
= 0xC0000;
606 /* Calculate the base address from the vector */
607 VideoRomBase
= (*((PULONG
)BaseAddress
+ 0x10) >> 12) & 0xFFFF0;
608 VideoRomBase
+= *((PULONG
)BaseAddress
+ 0x10) & 0xFFF0;
610 /* Now get to the actual ROM Start and make sure it's not invalid*/
611 VideoRomBase
&= 0xFFFF8000;
612 if (VideoRomBase
< 0xC0000) VideoRomBase
= 0xC0000;
614 /* And unmap the section */
615 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
618 /* Allocate BIOS Version pp Buffer */
619 BiosVersion
= ExAllocatePoolWithTag(PagedPool
, PAGE_SIZE
, TAG_CM
);
621 /* Setup settings to map the 64K BIOS ROM */
623 ViewSize
= 16 * PAGE_SIZE
;
624 ViewBase
.LowPart
= 0xF0000;
625 ViewBase
.HighPart
= 0;
628 Status
= ZwMapViewOfSection(SectionHandle
,
638 if (NT_SUCCESS(Status
))
640 /* Scan the ROM to get the BIOS Date */
641 if (CmpGetBiosDate(BaseAddress
, 16 * PAGE_SIZE
, Buffer
, TRUE
))
643 /* Convert it to Unicode */
644 RtlInitAnsiString(&TempString
, Buffer
);
645 RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
647 /* Write the date into the registry */
648 RtlInitUnicodeString(&ValueName
, L
"SystemBiosDate");
649 Status
= NtSetValueKey(SystemHandle
,
654 Data
.Length
+ sizeof(UNICODE_NULL
));
656 /* Free the string */
657 RtlFreeUnicodeString(&Data
);
661 /* Get the BIOS Date Identifier */
662 RtlCopyMemory(Buffer
, (PCHAR
)BaseAddress
+ (16*PAGE_SIZE
- 11), 8);
663 Buffer
[8] = ANSI_NULL
;
665 /* Convert it to unicode */
666 RtlInitAnsiString(&TempString
, Buffer
);
667 Status
= RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
668 if (NT_SUCCESS(Status
))
670 /* Save it to the registry */
671 Status
= NtSetValueKey(BiosHandle
,
676 Data
.Length
+ sizeof(UNICODE_NULL
));
678 /* ROS: Save a copy for bugzilla reporting */
679 RtlCreateUnicodeString(&KeRosBiosDate
, Data
.Buffer
);
681 /* Free the string */
682 RtlFreeUnicodeString(&Data
);
685 /* Close the bios information handle */
690 /* Get the BIOS Version */
691 if (CmpGetBiosVersion(BaseAddress
, 16* PAGE_SIZE
, Buffer
))
693 /* Start at the beginning of our buffer */
694 CurrentVersion
= BiosVersion
;
697 /* Convert to Unicode */
698 RtlInitAnsiString(&TempString
, Buffer
);
699 RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
701 /* Calculate the length of this string and copy it in */
702 Length
= Data
.Length
+ sizeof(UNICODE_NULL
);
703 RtlMoveMemory(CurrentVersion
, Data
.Buffer
, Length
);
705 /* Free the unicode string */
706 RtlFreeUnicodeString(&Data
);
708 /* Update the total length and see if we're out of space */
709 TotalLength
+= Length
;
710 if (TotalLength
+ 256 + sizeof(UNICODE_NULL
) > PAGE_SIZE
)
712 /* One more string would push us out, so stop here */
716 /* Go to the next string inside the multi-string buffer */
717 CurrentVersion
+= Length
;
719 /* Query the next BIOS Version */
720 } while (CmpGetBiosVersion(NULL
, 0, Buffer
));
722 /* Check if we found any strings at all */
725 /* Add the final null-terminator */
726 *(PWSTR
)CurrentVersion
= UNICODE_NULL
;
727 TotalLength
+= sizeof(UNICODE_NULL
);
729 /* Write the BIOS Version to the registry */
730 RtlInitUnicodeString(&ValueName
, L
"SystemBiosVersion");
731 Status
= NtSetValueKey(SystemHandle
,
738 /* ROS: Save a copy for bugzilla reporting */
739 RtlCreateUnicodeString(&KeRosBiosVersion
, (PWCH
)BiosVersion
);
743 /* Unmap the section */
744 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
747 /* Now prepare for Video BIOS Mapping of 32KB */
749 ViewSize
= 8 * PAGE_SIZE
;
750 ViewBase
.LowPart
= VideoRomBase
;
751 ViewBase
.HighPart
= 0;
754 Status
= ZwMapViewOfSection(SectionHandle
,
764 if (NT_SUCCESS(Status
))
766 /* Scan the ROM to get the BIOS Date */
767 if (CmpGetBiosDate(BaseAddress
, 8 * PAGE_SIZE
, Buffer
, FALSE
))
769 /* Convert it to Unicode */
770 RtlInitAnsiString(&TempString
, Buffer
);
771 RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
773 /* Write the date into the registry */
774 RtlInitUnicodeString(&ValueName
, L
"VideoBiosDate");
775 Status
= NtSetValueKey(SystemHandle
,
780 Data
.Length
+ sizeof(UNICODE_NULL
));
782 /* ROS: Save a copy for bugzilla reporting */
783 RtlCreateUnicodeString(&KeRosVideoBiosDate
, Data
.Buffer
);
785 /* Free the string */
786 RtlFreeUnicodeString(&Data
);
789 /* Get the Video BIOS Version */
790 if (CmpGetBiosVersion(BaseAddress
, 8* PAGE_SIZE
, Buffer
))
792 /* Start at the beginning of our buffer */
793 CurrentVersion
= BiosVersion
;
796 /* Convert to Unicode */
797 RtlInitAnsiString(&TempString
, Buffer
);
798 RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
800 /* Calculate the length of this string and copy it in */
801 Length
= Data
.Length
+ sizeof(UNICODE_NULL
);
802 RtlMoveMemory(CurrentVersion
, Data
.Buffer
, Length
);
804 /* Free the unicode string */
805 RtlFreeUnicodeString(&Data
);
807 /* Update the total length and see if we're out of space */
808 TotalLength
+= Length
;
809 if (TotalLength
+ 256 + sizeof(UNICODE_NULL
) > PAGE_SIZE
)
811 /* One more string would push us out, so stop here */
815 /* Go to the next string inside the multi-string buffer */
816 CurrentVersion
+= Length
;
818 /* Query the next BIOS Version */
819 } while (CmpGetBiosVersion(NULL
, 0, Buffer
));
821 /* Check if we found any strings at all */
824 /* Add the final null-terminator */
825 *(PWSTR
)CurrentVersion
= UNICODE_NULL
;
826 TotalLength
+= sizeof(UNICODE_NULL
);
828 /* Write the BIOS Version to the registry */
829 RtlInitUnicodeString(&ValueName
, L
"VideoBiosVersion");
830 Status
= NtSetValueKey(SystemHandle
,
837 /* ROS: Save a copy for bugzilla reporting */
838 RtlCreateUnicodeString(&KeRosVideoBiosVersion
, (PWCH
)BiosVersion
);
842 /* Unmap the section */
843 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
846 /* Close the section */
847 ZwClose(SectionHandle
);
849 /* Free the BIOS version string buffer */
850 if (BiosVersion
) ExFreePoolWithTag(BiosVersion
, TAG_CM
);
853 /* Close the processor handle */
855 return STATUS_SUCCESS
;