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
= "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 were 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}};
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
)) return Status
;
292 /* Create the BIOS Information key */
293 RtlInitUnicodeString(&KeyName
,
294 L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\"
295 L
"Control\\BIOSINFO");
296 InitializeObjectAttributes(&ObjectAttributes
,
298 OBJ_CASE_INSENSITIVE
,
301 Status
= NtCreateKey(&BiosHandle
,
306 REG_OPTION_NON_VOLATILE
,
308 if (ExpInTextModeSetup
)
310 if (!NT_SUCCESS(Status
))
313 else if (!NT_SUCCESS(Status
))
316 /* Create the CPU Key, and check if it already existed */
317 RtlInitUnicodeString(&KeyName
, L
"CentralProcessor");
318 InitializeObjectAttributes(&ObjectAttributes
,
320 OBJ_CASE_INSENSITIVE
,
323 Status
= NtCreateKey(&KeyHandle
,
324 KEY_READ
| KEY_WRITE
,
332 /* The key shouldn't already exist */
333 if (Disposition
== REG_CREATED_NEW_KEY
)
335 /* Allocate the configuration data for cmconfig.c */
336 CmpConfigurationData
= ExAllocatePoolWithTag(PagedPool
,
337 CmpConfigurationAreaSize
,
339 if (!CmpConfigurationData
) return STATUS_INSUFFICIENT_RESOURCES
;
342 for (i
= 0; i
< KeNumberProcessors
; i
++)
345 Prcb
= KiProcessorBlock
[i
];
347 /* Setup the Configuration Entry for the Processor */
348 RtlZeroMemory(&ConfigData
, sizeof (ConfigData
));
349 ConfigData
.ComponentEntry
.Class
= ProcessorClass
;
350 ConfigData
.ComponentEntry
.Type
= CentralProcessor
;
351 ConfigData
.ComponentEntry
.Key
= i
;
352 ConfigData
.ComponentEntry
.AffinityMask
= AFFINITY_MASK(i
);
353 ConfigData
.ComponentEntry
.Identifier
= Buffer
;
355 /* Check if the CPU doesn't support CPUID */
358 /* Build ID1-style string for older CPUs */
362 (Prcb
->CpuStep
>> 8) + 'A',
363 Prcb
->CpuStep
& 0xff);
367 /* Build ID2-style string for newer CPUs */
371 (Prcb
->CpuStep
>> 8),
372 Prcb
->CpuStep
& 0xff);
375 /* Save the ID string length now that we've created it */
376 ConfigData
.ComponentEntry
.IdentifierLength
= strlen(Buffer
) + 1;
378 /* Initialize the registry configuration node for it */
379 Status
= CmpInitializeRegistryNode(&ConfigData
,
382 InterfaceTypeUndefined
,
385 if (!NT_SUCCESS(Status
)) return(Status
);
388 /* Setup the Configuration Entry for the FPU */
389 RtlZeroMemory(&ConfigData
, sizeof(ConfigData
));
390 ConfigData
.ComponentEntry
.Class
= ProcessorClass
;
391 ConfigData
.ComponentEntry
.Type
= FloatingPointProcessor
;
392 ConfigData
.ComponentEntry
.Key
= i
;
393 ConfigData
.ComponentEntry
.AffinityMask
= AFFINITY_MASK(i
);
394 ConfigData
.ComponentEntry
.Identifier
= Buffer
;
396 /* For 386 cpus, the CPU pp is the identifier */
397 if (Prcb
->CpuType
== 3) strcpy(Buffer
, "80387");
399 /* Save the ID string length now that we've created it */
400 ConfigData
.ComponentEntry
.IdentifierLength
= strlen(Buffer
) + 1;
402 /* Initialize the registry configuration node for it */
403 Status
= CmpInitializeRegistryNode(&ConfigData
,
406 InterfaceTypeUndefined
,
409 if (!NT_SUCCESS(Status
))
411 /* Failed, close the CPU handle and return */
416 /* Close this new handle */
419 /* Stay on this CPU only */
420 KeSetSystemAffinityThread(Prcb
->SetMember
);
423 /* Uh oh, no CPUID! */
427 /* Check if we have extended CPUID that supports name ID */
428 //Ki386Cpuid(0x80000000, &ExtendedId, &Dummy, &Dummy, &Dummy);
429 if (ExtendedId
>= 0x80000004)
431 /* Do all the CPUIDs requred to get the full name */
432 PartialString
= CpuString
;
433 for (ExtendedId
= 2; ExtendedId
<= 4; ExtendedId
++)
436 /* Do the CPUID and save the name string */
437 Ki386Cpuid(0x80000000 | ExtendedId
,
438 (PULONG
)PartialString
,
439 (PULONG
)PartialString
+ 1,
440 (PULONG
)PartialString
+ 2,
441 (PULONG
)PartialString
+ 3);
444 /* Go to the next name string */
448 /* Null-terminate it */
449 CpuString
[48] = ANSI_NULL
;
453 /* Get the cache size while we're still localized */
454 CacheSize
= 0; //((PKIPCR)KeGetPcr())->SecondLevelCacheSize;
456 /* Go back to user affinity */
457 KeRevertToUserAffinityThread();
459 /* Check if we have a CPU Name */
462 /* Convert it to Unicode */
463 RtlInitAnsiString(&TempString
, CpuString
);
464 RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
466 /* Add it to the registry */
467 RtlInitUnicodeString(&ValueName
, L
"ProcessorNameString");
468 Status
= NtSetValueKey(KeyHandle
,
473 Data
.Length
+ sizeof(UNICODE_NULL
));
475 /* ROS: Save a copy for bugzilla reporting */
476 RtlCreateUnicodeString(&KeRosProcessorName
, Data
.Buffer
);
478 /* Free the temporary buffer */
479 RtlFreeUnicodeString(&Data
);
482 /* Check if we had a Vendor ID */
483 if (Prcb
->VendorString
)
485 /* Convert it to Unicode */
486 RtlInitAnsiString(&TempString
, Prcb
->VendorString
);
487 RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
489 /* Add it to the registry */
490 RtlInitUnicodeString(&ValueName
, L
"VendorIdentifier");
491 Status
= NtSetValueKey(KeyHandle
,
496 Data
.Length
+ sizeof(UNICODE_NULL
));
498 /* Free the temporary buffer */
499 RtlFreeUnicodeString(&Data
);
502 /* Check if we have features bits */
503 if (Prcb
->FeatureBits
)
505 /* Add them to the registry */
506 RtlInitUnicodeString(&ValueName
, L
"FeatureSet");
507 Status
= NtSetValueKey(KeyHandle
,
512 sizeof(Prcb
->FeatureBits
));
515 /* Check if we detected the CPU Speed */
518 /* Add it to the registry */
519 RtlInitUnicodeString(&ValueName
, L
"~MHz");
520 Status
= NtSetValueKey(KeyHandle
,
528 /* Check if we have an update signature */
529 if (Prcb
->UpdateSignature
.QuadPart
)
531 /* Add it to the registry */
532 RtlInitUnicodeString(&ValueName
, L
"Update Signature");
533 Status
= NtSetValueKey(KeyHandle
,
537 &Prcb
->UpdateSignature
,
538 sizeof(Prcb
->UpdateSignature
));
541 /* Close the processor handle */
544 /* FIXME: Detect CPU mismatches */
548 /* Free the configuration data */
549 ExFreePool(CmpConfigurationData
);
552 /* Open physical memory */
553 RtlInitUnicodeString(&SectionName
, L
"\\Device\\PhysicalMemory");
554 InitializeObjectAttributes(&ObjectAttributes
,
556 OBJ_CASE_INSENSITIVE
,
559 Status
= ZwOpenSection(&SectionHandle
,
562 if (!NT_SUCCESS(Status
)) goto Quickie
;
564 /* Map the first 1KB of memory to get the IVT */
565 ViewSize
= PAGE_SIZE
;
566 Status
= ZwMapViewOfSection(SectionHandle
,
576 if (!NT_SUCCESS(Status
))
579 VideoRomBase
= 0xC0000;
583 /* Calculate the base address from the vector */
584 VideoRomBase
= (*((PULONG
)BaseAddress
+ 0x10) >> 12) & 0xFFFF0;
585 VideoRomBase
+= *((PULONG
)BaseAddress
+ 0x10) & 0xFFF0;
587 /* Now get to the actual ROM Start and make sure it's not invalid*/
588 VideoRomBase
&= 0xFFFF8000;
589 if (VideoRomBase
< 0xC0000) VideoRomBase
= 0xC0000;
591 /* And unmap the section */
592 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
595 /* Allocate BIOS Version pp Buffer */
596 BiosVersion
= ExAllocatePoolWithTag(PagedPool
, PAGE_SIZE
, TAG_CM
);
598 /* Setup settings to map the 64K BIOS ROM */
600 ViewSize
= 16 * PAGE_SIZE
;
601 ViewBase
.LowPart
= 0xF0000;
602 ViewBase
.HighPart
= 0;
605 Status
= ZwMapViewOfSection(SectionHandle
,
615 if (NT_SUCCESS(Status
))
617 /* Scan the ROM to get the BIOS Date */
618 if (CmpGetBiosDate(BaseAddress
, 16 * PAGE_SIZE
, Buffer
, TRUE
))
620 /* Convert it to Unicode */
621 RtlInitAnsiString(&TempString
, Buffer
);
622 RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
624 /* Write the date into the registry */
625 RtlInitUnicodeString(&ValueName
, L
"SystemBiosDate");
626 Status
= NtSetValueKey(SystemHandle
,
631 Data
.Length
+ sizeof(UNICODE_NULL
));
633 /* Free the string */
634 RtlFreeUnicodeString(&Data
);
638 /* Get the BIOS Date Identifier */
639 RtlCopyMemory(Buffer
, (PCHAR
)BaseAddress
+ (16*PAGE_SIZE
- 11), 8);
640 Buffer
[8] = ANSI_NULL
;
642 /* Convert it to unicode */
643 RtlInitAnsiString(&TempString
, Buffer
);
644 Status
= RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
645 if (NT_SUCCESS(Status
))
647 /* Save it to the registry */
648 Status
= NtSetValueKey(BiosHandle
,
653 Data
.Length
+ sizeof(UNICODE_NULL
));
655 /* ROS: Save a copy for bugzilla reporting */
656 RtlCreateUnicodeString(&KeRosBiosDate
, Data
.Buffer
);
658 /* Free the string */
659 RtlFreeUnicodeString(&Data
);
662 /* Close the bios information handle */
667 /* Get the BIOS Version */
668 if (CmpGetBiosVersion(BaseAddress
, 16* PAGE_SIZE
, Buffer
))
670 /* Start at the beginning of our buffer */
671 CurrentVersion
= BiosVersion
;
674 /* Convert to Unicode */
675 RtlInitAnsiString(&TempString
, Buffer
);
676 RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
678 /* Calculate the length of this string and copy it in */
679 Length
= Data
.Length
+ sizeof(UNICODE_NULL
);
680 RtlMoveMemory(CurrentVersion
, Data
.Buffer
, Length
);
682 /* Free the unicode string */
683 RtlFreeUnicodeString(&Data
);
685 /* Update the total length and see if we're out of space */
686 TotalLength
+= Length
;
687 if (TotalLength
+ 256 + sizeof(UNICODE_NULL
) > PAGE_SIZE
)
689 /* One more string would push us out, so stop here */
693 /* Go to the next string inside the multi-string buffer */
694 CurrentVersion
+= Length
;
696 /* Query the next BIOS Version */
697 } while (CmpGetBiosVersion(NULL
, 0, Buffer
));
699 /* Check if we found any strings at all */
702 /* Add the final null-terminator */
703 *(PWSTR
)CurrentVersion
= UNICODE_NULL
;
704 TotalLength
+= sizeof(UNICODE_NULL
);
706 /* Write the BIOS Version to the registry */
707 RtlInitUnicodeString(&ValueName
, L
"SystemBiosVersion");
708 Status
= NtSetValueKey(SystemHandle
,
715 /* ROS: Save a copy for bugzilla reporting */
716 RtlCreateUnicodeString(&KeRosBiosVersion
, (PWCH
)BiosVersion
);
720 /* Unmap the section */
721 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
724 /* Now prepare for Video BIOS Mapping of 32KB */
726 ViewSize
= 8 * PAGE_SIZE
;
727 ViewBase
.LowPart
= VideoRomBase
;
728 ViewBase
.HighPart
= 0;
731 Status
= ZwMapViewOfSection(SectionHandle
,
741 if (NT_SUCCESS(Status
))
743 /* Scan the ROM to get the BIOS Date */
744 if (CmpGetBiosDate(BaseAddress
, 8 * PAGE_SIZE
, Buffer
, FALSE
))
746 /* Convert it to Unicode */
747 RtlInitAnsiString(&TempString
, Buffer
);
748 RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
750 /* Write the date into the registry */
751 RtlInitUnicodeString(&ValueName
, L
"VideoBiosDate");
752 Status
= NtSetValueKey(SystemHandle
,
757 Data
.Length
+ sizeof(UNICODE_NULL
));
759 /* ROS: Save a copy for bugzilla reporting */
760 RtlCreateUnicodeString(&KeRosVideoBiosDate
, Data
.Buffer
);
762 /* Free the string */
763 RtlFreeUnicodeString(&Data
);
766 /* Get the Video BIOS Version */
767 if (CmpGetBiosVersion(BaseAddress
, 8* PAGE_SIZE
, Buffer
))
769 /* Start at the beginning of our buffer */
770 CurrentVersion
= BiosVersion
;
773 /* Convert to Unicode */
774 RtlInitAnsiString(&TempString
, Buffer
);
775 RtlAnsiStringToUnicodeString(&Data
, &TempString
, TRUE
);
777 /* Calculate the length of this string and copy it in */
778 Length
= Data
.Length
+ sizeof(UNICODE_NULL
);
779 RtlMoveMemory(CurrentVersion
, Data
.Buffer
, Length
);
781 /* Free the unicode string */
782 RtlFreeUnicodeString(&Data
);
784 /* Update the total length and see if we're out of space */
785 TotalLength
+= Length
;
786 if (TotalLength
+ 256 + sizeof(UNICODE_NULL
) > PAGE_SIZE
)
788 /* One more string would push us out, so stop here */
792 /* Go to the next string inside the multi-string buffer */
793 CurrentVersion
+= Length
;
795 /* Query the next BIOS Version */
796 } while (CmpGetBiosVersion(NULL
, 0, Buffer
));
798 /* Check if we found any strings at all */
801 /* Add the final null-terminator */
802 *(PWSTR
)CurrentVersion
= UNICODE_NULL
;
803 TotalLength
+= sizeof(UNICODE_NULL
);
805 /* Write the BIOS Version to the registry */
806 RtlInitUnicodeString(&ValueName
, L
"VideoBiosVersion");
807 Status
= NtSetValueKey(SystemHandle
,
814 /* ROS: Save a copy for bugzilla reporting */
815 RtlCreateUnicodeString(&KeRosVideoBiosVersion
, (PWCH
)BiosVersion
);
819 /* Unmap the section */
820 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
823 /* Close the section */
824 ZwClose(SectionHandle
);
826 /* Free the BIOS version string buffer */
827 if (BiosVersion
) ExFreePool(BiosVersion
);
830 /* Close the procesor handle */
832 return STATUS_SUCCESS
;