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
, 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
= (ULONG
)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
= (ULONG
)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 /* Go back to user affinity */
455 KeRevertToUserAffinityThread();
457 /* Check if we have a CPU Name */
460 /* Convert it to Unicode */
461 RtlInitAnsiString(&TempString
, CpuString
);
462 RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
464 /* Add it to the registry */
465 RtlInitUnicodeString(&ValueName
, L
"ProcessorNameString");
466 Status
= NtSetValueKey(KeyHandle
,
471 Data
.Length
+ sizeof(UNICODE_NULL
));
473 /* ROS: Save a copy for bugzilla reporting */
474 RtlCreateUnicodeString(&KeRosProcessorName
, Data
.Buffer
);
476 /* Free the temporary buffer */
477 RtlFreeUnicodeString(&Data
);
480 /* Check if we had a Vendor ID */
481 if (Prcb
->VendorString
[0])
483 /* Convert it to Unicode */
484 RtlInitAnsiString(&TempString
, Prcb
->VendorString
);
485 RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
487 /* Add it to the registry */
488 RtlInitUnicodeString(&ValueName
, L
"VendorIdentifier");
489 Status
= NtSetValueKey(KeyHandle
,
494 Data
.Length
+ sizeof(UNICODE_NULL
));
496 /* Free the temporary buffer */
497 RtlFreeUnicodeString(&Data
);
500 /* Check if we have features bits */
501 if (Prcb
->FeatureBits
)
503 /* Add them to the registry */
504 RtlInitUnicodeString(&ValueName
, L
"FeatureSet");
505 Status
= NtSetValueKey(KeyHandle
,
510 sizeof(Prcb
->FeatureBits
));
513 /* Check if we detected the CPU Speed */
516 /* Add it to the registry */
517 RtlInitUnicodeString(&ValueName
, L
"~MHz");
518 Status
= NtSetValueKey(KeyHandle
,
526 /* Check if we have an update signature */
527 if (Prcb
->UpdateSignature
.QuadPart
)
529 /* Add it to the registry */
530 RtlInitUnicodeString(&ValueName
, L
"Update Signature");
531 Status
= NtSetValueKey(KeyHandle
,
535 &Prcb
->UpdateSignature
,
536 sizeof(Prcb
->UpdateSignature
));
539 /* Close the processor handle */
542 /* FIXME: Detect CPU mismatches */
546 /* Free the configuration data */
547 ExFreePoolWithTag(CmpConfigurationData
, TAG_CM
);
550 /* Open physical memory */
551 RtlInitUnicodeString(&SectionName
, L
"\\Device\\PhysicalMemory");
552 InitializeObjectAttributes(&ObjectAttributes
,
554 OBJ_CASE_INSENSITIVE
,
557 Status
= ZwOpenSection(&SectionHandle
,
560 if (!NT_SUCCESS(Status
)) goto Quickie
;
562 /* Map the first 1KB of memory to get the IVT */
563 ViewSize
= PAGE_SIZE
;
564 Status
= ZwMapViewOfSection(SectionHandle
,
574 if (!NT_SUCCESS(Status
))
577 VideoRomBase
= 0xC0000;
581 /* Calculate the base address from the vector */
582 VideoRomBase
= (*((PULONG
)BaseAddress
+ 0x10) >> 12) & 0xFFFF0;
583 VideoRomBase
+= *((PULONG
)BaseAddress
+ 0x10) & 0xFFF0;
585 /* Now get to the actual ROM Start and make sure it's not invalid*/
586 VideoRomBase
&= 0xFFFF8000;
587 if (VideoRomBase
< 0xC0000) VideoRomBase
= 0xC0000;
589 /* And unmap the section */
590 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
593 /* Allocate BIOS Version pp Buffer */
594 BiosVersion
= ExAllocatePoolWithTag(PagedPool
, PAGE_SIZE
, TAG_CM
);
596 /* Setup settings to map the 64K BIOS ROM */
598 ViewSize
= 16 * PAGE_SIZE
;
599 ViewBase
.LowPart
= 0xF0000;
600 ViewBase
.HighPart
= 0;
603 Status
= ZwMapViewOfSection(SectionHandle
,
613 if (NT_SUCCESS(Status
))
615 /* Scan the ROM to get the BIOS Date */
616 if (CmpGetBiosDate(BaseAddress
, 16 * PAGE_SIZE
, Buffer
, TRUE
))
618 /* Convert it to Unicode */
619 RtlInitAnsiString(&TempString
, Buffer
);
620 RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
622 /* Write the date into the registry */
623 RtlInitUnicodeString(&ValueName
, L
"SystemBiosDate");
624 Status
= NtSetValueKey(SystemHandle
,
629 Data
.Length
+ sizeof(UNICODE_NULL
));
631 /* Free the string */
632 RtlFreeUnicodeString(&Data
);
636 /* Get the BIOS Date Identifier */
637 RtlCopyMemory(Buffer
, (PCHAR
)BaseAddress
+ (16 * PAGE_SIZE
- 11), 8);
638 Buffer
[8] = ANSI_NULL
;
640 /* Convert it to unicode */
641 RtlInitAnsiString(&TempString
, Buffer
);
642 Status
= RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
643 if (NT_SUCCESS(Status
))
645 /* Save it to the registry */
646 Status
= NtSetValueKey(BiosHandle
,
651 Data
.Length
+ sizeof(UNICODE_NULL
));
653 /* ROS: Save a copy for bugzilla reporting */
654 RtlCreateUnicodeString(&KeRosBiosDate
, Data
.Buffer
);
656 /* Free the string */
657 RtlFreeUnicodeString(&Data
);
660 /* Close the bios information handle */
665 /* Get the BIOS Version */
666 if (CmpGetBiosVersion(BaseAddress
, 16 * PAGE_SIZE
, Buffer
))
668 /* Start at the beginning of our buffer */
669 CurrentVersion
= BiosVersion
;
672 /* Convert to Unicode */
673 RtlInitAnsiString(&TempString
, Buffer
);
674 RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
676 /* Calculate the length of this string and copy it in */
677 Length
= Data
.Length
+ sizeof(UNICODE_NULL
);
678 RtlMoveMemory(CurrentVersion
, Data
.Buffer
, Length
);
680 /* Free the unicode string */
681 RtlFreeUnicodeString(&Data
);
683 /* Update the total length and see if we're out of space */
684 TotalLength
+= Length
;
685 if (TotalLength
+ 256 + sizeof(UNICODE_NULL
) > PAGE_SIZE
)
687 /* One more string would push us out, so stop here */
691 /* Go to the next string inside the multi-string buffer */
692 CurrentVersion
+= Length
;
694 /* Query the next BIOS Version */
695 } while (CmpGetBiosVersion(NULL
, 0, Buffer
));
697 /* Check if we found any strings at all */
700 /* Add the final null-terminator */
701 *(PWSTR
)CurrentVersion
= UNICODE_NULL
;
702 TotalLength
+= sizeof(UNICODE_NULL
);
704 /* Write the BIOS Version to the registry */
705 RtlInitUnicodeString(&ValueName
, L
"SystemBiosVersion");
706 Status
= NtSetValueKey(SystemHandle
,
713 /* ROS: Save a copy for bugzilla reporting */
714 RtlCreateUnicodeString(&KeRosBiosVersion
, (PWCH
)BiosVersion
);
718 /* Unmap the section */
719 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
722 /* Now prepare for Video BIOS Mapping of 32KB */
724 ViewSize
= 8 * PAGE_SIZE
;
725 ViewBase
.QuadPart
= VideoRomBase
;
728 Status
= ZwMapViewOfSection(SectionHandle
,
738 if (NT_SUCCESS(Status
))
740 /* Scan the ROM to get the BIOS Date */
741 if (CmpGetBiosDate(BaseAddress
, 8 * PAGE_SIZE
, Buffer
, FALSE
))
743 /* Convert it to Unicode */
744 RtlInitAnsiString(&TempString
, Buffer
);
745 RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
747 /* Write the date into the registry */
748 RtlInitUnicodeString(&ValueName
, L
"VideoBiosDate");
749 Status
= NtSetValueKey(SystemHandle
,
754 Data
.Length
+ sizeof(UNICODE_NULL
));
756 /* ROS: Save a copy for bugzilla reporting */
757 RtlCreateUnicodeString(&KeRosVideoBiosDate
, Data
.Buffer
);
759 /* Free the string */
760 RtlFreeUnicodeString(&Data
);
763 /* Get the Video BIOS Version */
764 if (CmpGetBiosVersion(BaseAddress
, 8 * PAGE_SIZE
, Buffer
))
766 /* Start at the beginning of our buffer */
767 CurrentVersion
= BiosVersion
;
770 /* Convert to Unicode */
771 RtlInitAnsiString(&TempString
, Buffer
);
772 RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
774 /* Calculate the length of this string and copy it in */
775 Length
= Data
.Length
+ sizeof(UNICODE_NULL
);
776 RtlMoveMemory(CurrentVersion
, Data
.Buffer
, Length
);
778 /* Free the unicode string */
779 RtlFreeUnicodeString(&Data
);
781 /* Update the total length and see if we're out of space */
782 TotalLength
+= Length
;
783 if (TotalLength
+ 256 + sizeof(UNICODE_NULL
) > PAGE_SIZE
)
785 /* One more string would push us out, so stop here */
789 /* Go to the next string inside the multi-string buffer */
790 CurrentVersion
+= Length
;
792 /* Query the next BIOS Version */
793 } while (CmpGetBiosVersion(NULL
, 0, Buffer
));
795 /* Check if we found any strings at all */
798 /* Add the final null-terminator */
799 *(PWSTR
)CurrentVersion
= UNICODE_NULL
;
800 TotalLength
+= sizeof(UNICODE_NULL
);
802 /* Write the BIOS Version to the registry */
803 RtlInitUnicodeString(&ValueName
, L
"VideoBiosVersion");
804 Status
= NtSetValueKey(SystemHandle
,
811 /* ROS: Save a copy for bugzilla reporting */
812 RtlCreateUnicodeString(&KeRosVideoBiosVersion
, (PWCH
)BiosVersion
);
816 /* Unmap the section */
817 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
820 /* Close the section */
821 ZwClose(SectionHandle
);
823 /* Free the BIOS version string buffer */
824 if (BiosVersion
) ExFreePoolWithTag(BiosVersion
, TAG_CM
);
827 /* Close the procesor handle */
829 return STATUS_SUCCESS
;