How is the operating system able to know the battery level?

35

5

Even if we remove the default OS and install a new one it can interact with the battery. Are there some drivers for it? How does it work?

green

Posted 2018-10-05T09:44:15.313

Reputation: 469

Answers

33

The operating system interfaces with the firmware of an embedded controller that is part of the Advanced Configuration and Power Interface (ACPI).

Wikipedia defines it as :

In a computer, the Advanced Configuration and Power Interface (ACPI) provides an open standard that operating systems can use to discover and configure computer hardware components, to perform power management by (for example) putting unused components to sleep, and to perform status monitoring. First released in December 1996, ACPI aims to replace Advanced Power Management (APM), the MultiProcessor Specification, and the Plug and Play BIOS (PnP) Specification.[1] ACPI brings the power management under the control of the operating system, as opposed to the previous BIOS-centric system that relied on platform-specific firmware to determine power management and configuration policies.

Internally, ACPI advertises the available components and their functions to the operating system kernel using instruction lists ("methods") provided through the system firmware (Unified Extensible Firmware Interface (UEFI) or BIOS), which the kernel parses. ACPI then executes the desired operations (such as the initialization of hardware components) using an embedded minimal virtual machine.

The answer is then that a circuit or micro-chip is embedded in the motherboard, that itself contains a micro operating system which makes available some services via the computer firmware - UEFI or BIOS. It controls many aspects of power and device management.

The computer operating system has a system driver that is dedicated to interfacing with ACPI. Once ACPI is activated, it takes exclusive control of all aspects of power management and device configuration.

In many aspects ACPI is an operating system behind your operating system, except that it comes with the motherboard and is not under your control. There have been voices likening it to a Trojan horse and calling it a security risk. It may be disabled, but some computers may not boot without it, and advanced power management is then in any case disabled as well.

For more information about its use in Windows see the article Battery and power subsystem hardware design.

harrymc

Posted 2018-10-05T09:44:15.313

Reputation: 306 093

1

I might be wrong but it sounds like the last paragraph's description completely confuses ACPI with SMM and/or with Intel's ME/AMT. ACPI is mostly a static data structure, with small amounts of bytecode that the OS itself has to interpret, whereas ME is the actual system.

– user1686 – 2018-10-05T10:46:10.107

1@grawity: Don't think so. You may find a similar discussion in the Wikipedia ACPI article. – harrymc – 2018-10-05T10:47:50.207

1I don't see much discussion in there beyond a few quotes from one Shuttleworth's one blog post, and after reading both pages I'm not convinced in the slightest – it seems to me that he just lumps everything that comes with the system under the "ACPI" name, be it the BIOS or the Intel ME or whatever. As far as I know, out of all the garbage that comes with a modern system, ACPI is probably the only part that doesn't run on its own, and certainly not under the OS. – user1686 – 2018-10-05T11:00:26.447

2@grawity ACPI includes a firmware level code execution interface too though, that's how a lot of the firmware interaction involving it actually happens. On certain systems, this even uses SMM for the actual code execution, though that is thankfully getting less and less common. – Austin Hemmelgarn – 2018-10-05T17:44:52.213

@grawity: Yes, ACPI does nothing in the sense that it's only a controller. The UEFI/BIOS similarly manages devices and is placed higher in the hardware hierarchy, and one of them is the ACPI. Functions are externalized at each level, also becoming more and more generalized with each level. Software such as operating systems is designed in much the same approach of levels of generalization, which is how humans attack complex problems. – harrymc – 2018-10-05T20:41:52.560

The answer here is the most correct. Acpi does though consist largely of a look up table. Gravity is confusing discussion about Linux founder's distaste for uefi and not acpi. Acpi is a logical solution while uefi is a a very bloated bios. Also uefi essentially makes Linux somewhat useless which motivates Linus being against it. Acpi and uefi are abstractly unrelated to intel me. Intel me is a secure execution engine that interacts with all aspects of the firmware which makes up bios, uefi or o.s. – marshal craft – 2018-10-06T10:06:58.443

To be short (this is all very much more to it than this) intel me verifies every thing that is run by the processor. Additionally it includes microcode which is updated by intel remotely. The microcode runs first. The processor after and while running me microcode, loads first none intel code from spi flash. The flash has a flash descriptor section which is analogous to a table of contents. Here the firmware developers can control the structure of the flash. It does and must contain a secure execution section which typically must verify the firmware is indeed signed by a trusted party. – marshal craft – 2018-10-06T10:14:45.247

The signatures etc, signing mechanisms are stored in secure me registers which have fuses so they become read only. Apple notably did not bother to configure these correctly allowing remote users to alter and use these features maliciously, though it was reported to be intels fault it was simply apple not bothering to configure these. Anyways most of this is undocumented in public domain. Intel chooses oems and provides them with documentation. Additionally the secure execution engine can run java like scrypts, access wireless networks, and even run while the pc is off. – marshal craft – 2018-10-06T10:20:06.780

Acpi and uefi are two primary documents defining necessary firmware to bring up a Windows device. Both are very well documented and in public domain. Intel is primary developer of uefi documentation AND UEFI firmware itself. Microsoft is primarily responsible for developing Acpi. As such this describes some what what they pertain to more. Ounce the windows or Linux is bootliader is executed (as a uefi script) the os takes over from that point and processor switches execution states and uefi is done. This is why uefi is kind of stupid. – marshal craft – 2018-10-06T10:24:03.460

Were talking about very advanced bios that is basically a stand alone os/bios capable of running portable executables. Chrome books are basically uefi running chrome.exe. All this complexity to simply check hardware and load os and then never do anything until next reboot. – marshal craft – 2018-10-06T10:28:28.843

Acpi on the other hand is used by uefi mandatory and os drivers. Uefi now has and requires drivers however using uefi drivers for Windows will reduce performance. Windows really needs purpose built drivers but can make due. Acpi consists primarily of a table and aml which is basically hardware specific assembly. – marshal craft – 2018-10-06T10:30:54.593

More importantly the root driver in windows is ACPI.SYS all other drivers are child to this driver. This driver is a fundamental part of the windows kernal driver framework. For example the pci bus would be a bus driver and loaded by acpi.sys – marshal craft – 2018-10-06T10:33:26.307

@marshalcraft: Nowhere in my posts did I refer to Linus Torvalds (the creator of Linux). Mark Shuttleworth is the founder of the Ubuntu Linux distribution, but he didn't create Linux itself. – user1686 – 2018-10-07T15:17:33.460

Better question is where at any point in this discussion does it matter who developed Linux? That's neither here more there. This is about acpi. Linus primarily has publically expressed concern about uefi. The reason I made this point is because there is plenty of reason to object to the use of uefi. Acpi on the other hand serves more provable purpose. But the two are related and uefi requires acpi. – marshal craft – 2018-10-08T10:42:35.897

Im not trying to discredit anyone but provide a more accurate description. I believe this is a core issue that effects humanity. No one can question the role intel has played in human society. Currently intels treatment of its txe (formally me) is a critical issue. Uefi, and acpi are fundamentally related however the core issue is the trusted execution engine. – marshal craft – 2018-10-08T10:46:31.643

Txe has several questionable ethics problems. One is the introduction of the desire to use the customers property with out their consent, let alone knowledge at all. But a more pressing issue is the artificial barrier to entry for oem developers. This culminates in a techtatorship. With fake free market, fake innovation, just a giant Hollywood set. Where only a pico percent of the population have any real ability to innovate and play a role in society. – marshal craft – 2018-10-08T10:51:33.007

Make no doubt about it this is but a fake hollywood set, a carefully set up game of monopoly, where the outcome is carefully calculated apriori. The only real solution though is the reduction in costs of semiconductor development, which can only come about by a large increase of public awareness of the chemical/physical world around them and a greater awareness of the capabilities of their own mind. – marshal craft – 2018-10-08T10:56:37.027

57

As a complement to the other answer, how does the software running on the computer know what the battery level is? It asks the battery.

Most laptop batteries are smart batteries that have their own microcontroller or "fuel gauge" ASIC, which the host can communicate with over SMBus. People have reverse engineered some examples.

The SMBus may or may not be exposed directly to the operating system in a way that allows the administrator to query it directly. There are various programs like OpenHardwareMonitor or Speccy or lm-sensors that can interrogate the bus to find out about the hardware.

pjc50

Posted 2018-10-05T09:44:15.313

Reputation: 5 786

great explanation to understand the basic concept in general! Although I would ad quotation marks around the word "asks". – Albin – 2018-10-05T11:23:06.393

There is no conflict with my answer : SMBus is a component that is managed by ACPI for mobile computers in which it exists. – harrymc – 2018-10-05T11:55:54.020

1This smart battery interface can also be exposed through more standard means, such as USB. For instance, many UPSs have a USB port on them that, when plugged into the computer, tells the computer how much power is left and displays it just like a laptop does. – TheHansinator – 2018-10-05T13:55:45.107

2@TheHansinator I would argue that SMBus is the standard means for communicating with a smart battery. Almost all phone's and laptops use it, which is a whole lot more systems than are using external UPS devices. – Austin Hemmelgarn – 2018-10-05T17:41:09.933

@AustinHemmelgarn True. Maybe a better word is "conventional", in that the system is more or less an OS abstraction that devices besides embedded batteries can use. – TheHansinator – 2018-10-05T17:43:29.700

2

More generally, all computer chips have documentation which tells designers and programmers what they do and how to configure them to do it. Low level access to these chips can be done by reading and writing directly to the chip's registers.

More complicated chips can come with a software program called a 'driver' that allows high level access to the operating system or even other applications. Your smart phone for example has an Application Programming Interface (API) that allows access to much of the phone's hardware, GPS, accelerometer, battery, camera, etc. When you write an "App", you can access these hardware devices using the API just as the operating system can (though the OS usually has more extensive access than a program running in 'user space'.)

These interfaces are very well defined so that when you make a function call to the hardware chip, it will respond with the information requested. The battery controller chip is just one example of this generic infrastructure.

Whomever writes the operating system code gets the documentation for the chip and writes software to communicate with the chip and retrieve the information it desires.

So the next time you load a 'driver' (or it is done automatically) for an external hard drive, USB thumb drive, or something else, you'll understand a bit more about how things happen 'under the hood'.

CramerTV

Posted 2018-10-05T09:44:15.313

Reputation: 382