2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/config/i386/cmhardwr.c
5 * PURPOSE: Configuration Manager - Hardware-Specific Code
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
9 /* INCLUDES ******************************************************************/
15 /* GLOBALS *******************************************************************/
17 PCHAR CmpID1
= "80%u86-%c%x";
18 PCHAR CmpID2
= "x86 Family %u Model %u Stepping %u";
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
, Length
, TotalLength
= 0, i
, Disposition
;
238 HANDLE KeyHandle
, BiosHandle
, SystemHandle
, FpuHandle
, SectionHandle
;
239 CONFIGURATION_COMPONENT_DATA ConfigData
;
241 ULONG ExtendedId
, Dummy
;
243 USHORT IndexTable
[MaximumType
+ 1] = {0};
244 ANSI_STRING TempString
;
245 PCHAR PartialString
= NULL
, BiosVersion
;
247 PVOID BaseAddress
= NULL
;
248 LARGE_INTEGER ViewBase
= {{0, 0}};
249 ULONG_PTR VideoRomBase
;
250 PCHAR CurrentVersion
;
251 extern UNICODE_STRING KeRosProcessorName
, KeRosBiosDate
, KeRosBiosVersion
;
252 extern UNICODE_STRING KeRosVideoBiosDate
, KeRosVideoBiosVersion
;
254 /* Open the SMSS Memory Management key */
255 RtlInitUnicodeString(&KeyName
,
256 L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\"
257 L
"Control\\Session Manager\\Memory Management");
258 InitializeObjectAttributes(&ObjectAttributes
,
260 OBJ_CASE_INSENSITIVE
,
263 Status
= NtOpenKey(&KeyHandle
, KEY_READ
| KEY_WRITE
, &ObjectAttributes
);
264 if (NT_SUCCESS(Status
))
266 /* Detect if PAE is enabled */
267 HavePae
= SharedUserData
->ProcessorFeatures
[PF_PAE_ENABLED
];
270 RtlInitUnicodeString(&ValueName
, L
"PhysicalAddressExtension");
271 NtSetValueKey(KeyHandle
,
282 /* Open the hardware description key */
283 RtlInitUnicodeString(&KeyName
,
284 L
"\\Registry\\Machine\\Hardware\\Description\\System");
285 InitializeObjectAttributes(&ObjectAttributes
,
287 OBJ_CASE_INSENSITIVE
,
290 Status
= NtOpenKey(&SystemHandle
, KEY_READ
| KEY_WRITE
, &ObjectAttributes
);
291 if (!NT_SUCCESS(Status
)) return 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
))
317 /* Create the CPU Key, and check if it already existed */
318 RtlInitUnicodeString(&KeyName
, L
"CentralProcessor");
319 InitializeObjectAttributes(&ObjectAttributes
,
321 OBJ_CASE_INSENSITIVE
,
324 Status
= NtCreateKey(&KeyHandle
,
325 KEY_READ
| KEY_WRITE
,
333 /* The key shouldn't already exist */
334 if (Disposition
== REG_CREATED_NEW_KEY
)
336 /* Allocate the configuration data for cmconfig.c */
337 CmpConfigurationData
= ExAllocatePoolWithTag(PagedPool
,
338 CmpConfigurationAreaSize
,
340 if (!CmpConfigurationData
) return STATUS_INSUFFICIENT_RESOURCES
;
343 for (i
= 0; i
< KeNumberProcessors
; i
++)
346 Prcb
= KiProcessorBlock
[i
];
348 /* Setup the Configuration Entry for the Processor */
349 RtlZeroMemory(&ConfigData
, sizeof (ConfigData
));
350 ConfigData
.ComponentEntry
.Class
= ProcessorClass
;
351 ConfigData
.ComponentEntry
.Type
= CentralProcessor
;
352 ConfigData
.ComponentEntry
.Key
= i
;
353 ConfigData
.ComponentEntry
.AffinityMask
= AFFINITY_MASK(i
);
354 ConfigData
.ComponentEntry
.Identifier
= Buffer
;
356 /* Check if the CPU doesn't support CPUID */
359 /* Build ID1-style string for older CPUs */
363 (Prcb
->CpuStep
>> 8) + 'A',
364 Prcb
->CpuStep
& 0xff);
368 /* Build ID2-style string for newer CPUs */
372 (Prcb
->CpuStep
>> 8),
373 Prcb
->CpuStep
& 0xff);
376 /* Save the ID string length now that we've created it */
377 ConfigData
.ComponentEntry
.IdentifierLength
= strlen(Buffer
) + 1;
379 /* Initialize the registry configuration node for it */
380 Status
= CmpInitializeRegistryNode(&ConfigData
,
383 InterfaceTypeUndefined
,
386 if (!NT_SUCCESS(Status
)) return(Status
);
388 /* Check if we have an FPU */
389 if (KeI386NpxPresent
)
391 /* Setup the Configuration Entry for the FPU */
392 RtlZeroMemory(&ConfigData
, sizeof(ConfigData
));
393 ConfigData
.ComponentEntry
.Class
= ProcessorClass
;
394 ConfigData
.ComponentEntry
.Type
= FloatingPointProcessor
;
395 ConfigData
.ComponentEntry
.Key
= i
;
396 ConfigData
.ComponentEntry
.AffinityMask
= AFFINITY_MASK(i
);
397 ConfigData
.ComponentEntry
.Identifier
= Buffer
;
399 /* For 386 cpus, the CPU pp is the identifier */
400 if (Prcb
->CpuType
== 3) strcpy(Buffer
, "80387");
402 /* Save the ID string length now that we've created it */
403 ConfigData
.ComponentEntry
.IdentifierLength
= strlen(Buffer
) + 1;
405 /* Initialize the registry configuration node for it */
406 Status
= CmpInitializeRegistryNode(&ConfigData
,
409 InterfaceTypeUndefined
,
412 if (!NT_SUCCESS(Status
))
414 /* Failed, close the CPU handle and return */
419 /* Close this new handle */
422 /* Stay on this CPU only */
423 KeSetSystemAffinityThread(Prcb
->SetMember
);
426 /* Uh oh, no CPUID! */
430 /* Check if we have extended CPUID that supports name ID */
431 CPUID(0x80000000, &ExtendedId
, &Dummy
, &Dummy
, &Dummy
);
432 if (ExtendedId
>= 0x80000004)
434 /* Do all the CPUIDs required to get the full name */
435 PartialString
= CpuString
;
436 for (ExtendedId
= 2; ExtendedId
<= 4; ExtendedId
++)
438 /* Do the CPUID and save the name string */
439 CPUID(0x80000000 | ExtendedId
,
440 (PULONG
)PartialString
,
441 (PULONG
)PartialString
+ 1,
442 (PULONG
)PartialString
+ 2,
443 (PULONG
)PartialString
+ 3);
445 /* Go to the next name string */
449 /* Null-terminate it */
450 CpuString
[47] = ANSI_NULL
;
454 /* Get the cache size while we're still localized */
455 CacheSize
= ((PKIPCR
)KeGetPcr())->SecondLevelCacheSize
;
457 /* Go back to user affinity */
458 KeRevertToUserAffinityThread();
460 /* Check if we have a CPU Name */
463 /* Convert it to Unicode */
464 RtlInitAnsiString(&TempString
, CpuString
);
465 RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
467 /* Add it to the registry */
468 RtlInitUnicodeString(&ValueName
, L
"ProcessorNameString");
469 Status
= NtSetValueKey(KeyHandle
,
474 Data
.Length
+ sizeof(UNICODE_NULL
));
476 /* ROS: Save a copy for bugzilla reporting */
477 RtlCreateUnicodeString(&KeRosProcessorName
, Data
.Buffer
);
479 /* Free the temporary buffer */
480 RtlFreeUnicodeString(&Data
);
483 /* Check if we had a Vendor ID */
484 if (Prcb
->VendorString
)
486 /* Convert it to Unicode */
487 RtlInitAnsiString(&TempString
, Prcb
->VendorString
);
488 RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
490 /* Add it to the registry */
491 RtlInitUnicodeString(&ValueName
, L
"VendorIdentifier");
492 Status
= NtSetValueKey(KeyHandle
,
497 Data
.Length
+ sizeof(UNICODE_NULL
));
499 /* Free the temporary buffer */
500 RtlFreeUnicodeString(&Data
);
503 /* Check if we have features bits */
504 if (Prcb
->FeatureBits
)
506 /* Add them to the registry */
507 RtlInitUnicodeString(&ValueName
, L
"FeatureSet");
508 Status
= NtSetValueKey(KeyHandle
,
513 sizeof(Prcb
->FeatureBits
));
516 /* Check if we detected the CPU Speed */
519 /* Add it to the registry */
520 RtlInitUnicodeString(&ValueName
, L
"~MHz");
521 Status
= NtSetValueKey(KeyHandle
,
529 /* Check if we have an update signature */
530 if (Prcb
->UpdateSignature
.QuadPart
)
532 /* Add it to the registry */
533 RtlInitUnicodeString(&ValueName
, L
"Update Signature");
534 Status
= NtSetValueKey(KeyHandle
,
538 &Prcb
->UpdateSignature
,
539 sizeof(Prcb
->UpdateSignature
));
542 /* Close the processor handle */
545 /* FIXME: Detect CPU mismatches */
549 /* Free the configuration data */
550 ExFreePoolWithTag(CmpConfigurationData
, TAG_CM
);
553 /* Open physical memory */
554 RtlInitUnicodeString(&SectionName
, L
"\\Device\\PhysicalMemory");
555 InitializeObjectAttributes(&ObjectAttributes
,
557 OBJ_CASE_INSENSITIVE
,
560 Status
= ZwOpenSection(&SectionHandle
,
563 if (!NT_SUCCESS(Status
)) goto Quickie
;
565 /* Map the first 1KB of memory to get the IVT */
566 ViewSize
= PAGE_SIZE
;
567 Status
= ZwMapViewOfSection(SectionHandle
,
577 if (!NT_SUCCESS(Status
))
580 VideoRomBase
= 0xC0000;
584 /* Calculate the base address from the vector */
585 VideoRomBase
= (*((PULONG
)BaseAddress
+ 0x10) >> 12) & 0xFFFF0;
586 VideoRomBase
+= *((PULONG
)BaseAddress
+ 0x10) & 0xFFF0;
588 /* Now get to the actual ROM Start and make sure it's not invalid*/
589 VideoRomBase
&= 0xFFFF8000;
590 if (VideoRomBase
< 0xC0000) VideoRomBase
= 0xC0000;
592 /* And unmap the section */
593 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
596 /* Allocate BIOS Version pp Buffer */
597 BiosVersion
= ExAllocatePoolWithTag(PagedPool
, PAGE_SIZE
, TAG_CM
);
599 /* Setup settings to map the 64K BIOS ROM */
601 ViewSize
= 16 * PAGE_SIZE
;
602 ViewBase
.LowPart
= 0xF0000;
603 ViewBase
.HighPart
= 0;
606 Status
= ZwMapViewOfSection(SectionHandle
,
616 if (NT_SUCCESS(Status
))
618 /* Scan the ROM to get the BIOS Date */
619 if (CmpGetBiosDate(BaseAddress
, 16 * PAGE_SIZE
, Buffer
, TRUE
))
621 /* Convert it to Unicode */
622 RtlInitAnsiString(&TempString
, Buffer
);
623 RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
625 /* Write the date into the registry */
626 RtlInitUnicodeString(&ValueName
, L
"SystemBiosDate");
627 Status
= NtSetValueKey(SystemHandle
,
632 Data
.Length
+ sizeof(UNICODE_NULL
));
634 /* Free the string */
635 RtlFreeUnicodeString(&Data
);
639 /* Get the BIOS Date Identifier */
640 RtlCopyMemory(Buffer
, (PCHAR
)BaseAddress
+ (16 * PAGE_SIZE
- 11), 8);
641 Buffer
[8] = ANSI_NULL
;
643 /* Convert it to unicode */
644 RtlInitAnsiString(&TempString
, Buffer
);
645 Status
= RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
646 if (NT_SUCCESS(Status
))
648 /* Save it to the registry */
649 Status
= NtSetValueKey(BiosHandle
,
654 Data
.Length
+ sizeof(UNICODE_NULL
));
656 /* ROS: Save a copy for bugzilla reporting */
657 RtlCreateUnicodeString(&KeRosBiosDate
, Data
.Buffer
);
659 /* Free the string */
660 RtlFreeUnicodeString(&Data
);
663 /* Close the bios information handle */
668 /* Get the BIOS Version */
669 if (CmpGetBiosVersion(BaseAddress
, 16 * PAGE_SIZE
, Buffer
))
671 /* Start at the beginning of our buffer */
672 CurrentVersion
= BiosVersion
;
675 /* Convert to Unicode */
676 RtlInitAnsiString(&TempString
, Buffer
);
677 RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
679 /* Calculate the length of this string and copy it in */
680 Length
= Data
.Length
+ sizeof(UNICODE_NULL
);
681 RtlMoveMemory(CurrentVersion
, Data
.Buffer
, Length
);
683 /* Free the unicode string */
684 RtlFreeUnicodeString(&Data
);
686 /* Update the total length and see if we're out of space */
687 TotalLength
+= Length
;
688 if (TotalLength
+ 256 + sizeof(UNICODE_NULL
) > PAGE_SIZE
)
690 /* One more string would push us out, so stop here */
694 /* Go to the next string inside the multi-string buffer */
695 CurrentVersion
+= Length
;
697 /* Query the next BIOS Version */
698 } while (CmpGetBiosVersion(NULL
, 0, Buffer
));
700 /* Check if we found any strings at all */
703 /* Add the final null-terminator */
704 *(PWSTR
)CurrentVersion
= UNICODE_NULL
;
705 TotalLength
+= sizeof(UNICODE_NULL
);
707 /* Write the BIOS Version to the registry */
708 RtlInitUnicodeString(&ValueName
, L
"SystemBiosVersion");
709 Status
= NtSetValueKey(SystemHandle
,
716 /* ROS: Save a copy for bugzilla reporting */
717 RtlCreateUnicodeString(&KeRosBiosVersion
, (PWCH
)BiosVersion
);
721 /* Unmap the section */
722 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
725 /* Now prepare for Video BIOS Mapping of 32KB */
727 ViewSize
= 8 * PAGE_SIZE
;
728 ViewBase
.LowPart
= VideoRomBase
;
729 ViewBase
.HighPart
= 0;
732 Status
= ZwMapViewOfSection(SectionHandle
,
742 if (NT_SUCCESS(Status
))
744 /* Scan the ROM to get the BIOS Date */
745 if (CmpGetBiosDate(BaseAddress
, 8 * PAGE_SIZE
, Buffer
, FALSE
))
747 /* Convert it to Unicode */
748 RtlInitAnsiString(&TempString
, Buffer
);
749 RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
751 /* Write the date into the registry */
752 RtlInitUnicodeString(&ValueName
, L
"VideoBiosDate");
753 Status
= NtSetValueKey(SystemHandle
,
758 Data
.Length
+ sizeof(UNICODE_NULL
));
760 /* ROS: Save a copy for bugzilla reporting */
761 RtlCreateUnicodeString(&KeRosVideoBiosDate
, Data
.Buffer
);
763 /* Free the string */
764 RtlFreeUnicodeString(&Data
);
767 /* Get the Video BIOS Version */
768 if (CmpGetBiosVersion(BaseAddress
, 8 * PAGE_SIZE
, Buffer
))
770 /* Start at the beginning of our buffer */
771 CurrentVersion
= BiosVersion
;
774 /* Convert to Unicode */
775 RtlInitAnsiString(&TempString
, Buffer
);
776 RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
778 /* Calculate the length of this string and copy it in */
779 Length
= Data
.Length
+ sizeof(UNICODE_NULL
);
780 RtlMoveMemory(CurrentVersion
, Data
.Buffer
, Length
);
782 /* Free the unicode string */
783 RtlFreeUnicodeString(&Data
);
785 /* Update the total length and see if we're out of space */
786 TotalLength
+= Length
;
787 if (TotalLength
+ 256 + sizeof(UNICODE_NULL
) > PAGE_SIZE
)
789 /* One more string would push us out, so stop here */
793 /* Go to the next string inside the multi-string buffer */
794 CurrentVersion
+= Length
;
796 /* Query the next BIOS Version */
797 } while (CmpGetBiosVersion(NULL
, 0, Buffer
));
799 /* Check if we found any strings at all */
802 /* Add the final null-terminator */
803 *(PWSTR
)CurrentVersion
= UNICODE_NULL
;
804 TotalLength
+= sizeof(UNICODE_NULL
);
806 /* Write the BIOS Version to the registry */
807 RtlInitUnicodeString(&ValueName
, L
"VideoBiosVersion");
808 Status
= NtSetValueKey(SystemHandle
,
815 /* ROS: Save a copy for bugzilla reporting */
816 RtlCreateUnicodeString(&KeRosVideoBiosVersion
, (PWCH
)BiosVersion
);
820 /* Unmap the section */
821 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
824 /* Close the section */
825 ZwClose(SectionHandle
);
827 /* Free the BIOS version string buffer */
828 if (BiosVersion
) ExFreePoolWithTag(BiosVersion
, TAG_CM
);
831 /* Close the procesor handle */
833 return STATUS_SUCCESS
;