Commit Graph

19 Commits

Author SHA1 Message Date
Frinzell, Aaron 2181416bd9 Update type hints
Signed-off-by: Frinzell, Aaron <aaron.frinzell@intel.com>
2024-09-10 16:38:28 -07:00
Frinzell, Aaron 926f9da4f6 Helper fstrings
Signed-off-by: Frinzell, Aaron <aaron.frinzell@intel.com>
2024-08-28 13:58:42 -07:00
Dan Scott f19760c63c Replace print() with logger.log()
Signed-off-by: Dan Scott <dan.scott@intel.com>
2024-08-05 16:35:54 -07:00
Nathaniel Mitchell ed9c6e2896 Update how ACPI Tables are found
Signed-off-by: Nathaniel Mitchell <nathaniel.p.mitchell@intel.com>
2024-04-17 15:27:21 -07:00
Dan Scott 0e8192cfd0 Move files to library folder 2024-03-22 14:26:59 -07:00
Dan Scott a933b1337d Update references to functions in new library files 2024-03-22 14:26:59 -07:00
Nathaniel Mitchell 27ee5eeb66 Clean up helper create/start/stop/delete functions
Signed-off-by: Nathaniel Mitchell <nathaniel.p.mitchell@intel.com>
2023-07-19 16:33:59 -07:00
Frinzell, Aaron 41ecf2cdb1 Implement basehelper.py as an ABC
Signed-off-by: Frinzell, Aaron <aaron.frinzell@intel.com>
2023-06-23 08:27:17 -07:00
Nicolas Iooss 6d52c2c54f Add type hints to helper.linuxnative.cpuid
With these annotations, mypy no longer report any issues in strict mode:

    $ mypy --strict chipsec/helper/linuxnative/cpuid.py
    Success: no issues found in 1 source file

Signed-off-by: Nicolas Iooss <nicolas.iooss_git@polytechnique.org>
2023-06-20 16:37:01 -07:00
Nicolas Iooss f0ebbe01cc Simplify CPUID code mapping in linuxnative helper
Make the machine code `bytes` instead of `list`. This makes it no longer
necessary to cast it to `c_ubyte * size` before using it.

While at it, add `mmap.MAP_PRIVATE` flag to the mapping, as it is not
needed to create a `MAP_SHARED` mapping for the code (which is the
default flag).

Signed-off-by: Nicolas Iooss <nicolas.iooss_git@polytechnique.org>
2023-06-20 16:37:01 -07:00
Nicolas Iooss fb80548746 Remove print_function import
Now that chipsec is no longer compatible with Python2, this import is no
longer useful.

Signed-off-by: Nicolas Iooss <nicolas.iooss_git@polytechnique.org>
2023-06-20 16:37:01 -07:00
Nicolas Iooss 14c5f7c314 Directly import chipsec.helper.linuxnative.cpuid in linuxnative
There is no point in doing a lazy-import with
`chipsec.helper.linuxnative.cpuid`, as the `cpuid` function is almost
always called when starting chipsec, to display the banner.

Signed-off-by: Nicolas Iooss <nicolas.iooss_git@polytechnique.org>
2023-06-20 16:37:01 -07:00
Nicolas Iooss 470820d464 Use c_void_p instead of c_voidp
`ctypes.c_voidp` was a bug which is kept in ctypes for backward
compatibility. This is documented in
https://github.com/python/cpython/blob/3.10/Lib/ctypes/__init__.py#L246
:

    c_voidp = c_void_p # backwards compatibility (to a bug)

Replace the use of `c_voidp` with `c_void_p`.

Signed-off-by: Nicolas Iooss <nicolas.iooss_git@polytechnique.org>
2023-06-20 16:37:01 -07:00
Nicolas Iooss 09f421639e Rework legacy PCI helper
When using chipsec.chipset Python module from an unprivileged user, the
Python interpreter crashes with a segmentation fault on an `outl`
instruction. This is because `iopl(3)` failed and the "legacy PCI"
implementation tried to write a value to an I/O port without being
allowed to.

Fix this by checking the return value of `iopl`.

While at it, enable using `error` in the C library, to properly report
the `EPERM` error.

While working on this code, it appeared that:

- `self.addr` was written twice in `PORTS`' constructor: once for the
  `inl` implementation and another one for `outl`.
- Everytime `PORTS` or `LEGACY_PCI` objects were created, some memory
  was allocated (with `mmap.mmap`) to run the helpers and this memory
  was never freed.
- The global variables `IN_PORT` and `OUT_PORT` were defined as lists
  but always used as bytes.

Fix these issues by spliting `self.addr` into two variables, by
introducing a class property which re-use previous `PORTS` instances
(making this class a singleton) and by making `IN_PORT` and `OUT_PORT`
directly `bytes`.

While at it, change the names to the usual camel case convention
(`PORTS` -> `Ports` and `LEGACY_PCI` -> `LegacyPci`) and add type hints.

Signed-off-by: Nicolas Iooss <nicolas.iooss_git@polytechnique.org>
2023-06-14 16:38:57 -07:00
Nicolas Iooss c2bcf585ec Fix Linux native helper MMIO read/write
Reading 64-bit MMIO registers does not work with Linux native helper.
For example:

    $ ./chipsec_util.py --helper linuxnativehelper txt state
    ...
    TXT Public Key Hash: 9c78f0d8000000002f47761c00000000164a66a90000000092e3144f00000000

This value comes from four 64-bit MMIO registers (`TXT_PUBLIC_KEY_0` to
`TXT_PUBLIC_KEY_3`). This is caused by `LinuxNativeHelper.read_mmio_reg`
assuming the size cannot be larger than 4. And it also assumes that the
physical address is always aligned on a 4-byte boundary.

Reading a value as a "32-bit integer" can be important for MMIO
registers, so do not change this case, when the physical address is
aligned. In practice:

- If `size == 1`, the result can be directly read from the mapping.
- Otherwise, if the address is aligned,
  `region_mv.cast(defines.SIZE2FORMAT[size])` can be used to trigger an
  atomic read of the requested size.
- In the unaligned case, the bytes are read from the memory view,
  possibly one by one, and `defines.unpack1` is used to glue them back
  to an integer.

Implement a similar logic in `write_mmio_reg` too. While at it, as the
`return 0` did not make much sense in this function, remove it.

With this commit:

    $ ./chipsec_util.py --helper linuxnativehelper txt state
    ...
    TXT Public Key Hash: 9c78f0d853de854a2f47761c72b86a11164a66a984c1aad792e3144fb71c2d11

Signed-off-by: Nicolas Iooss <nicolas.iooss_git@polytechnique.org>
2023-06-05 16:44:00 -07:00
Nathaniel Mitchell 4b38d41a29 Move helper.getcwd() from *helper to oshelper
Signed-off-by: Nathaniel Mitchell <nathaniel.p.mitchell@intel.com>
2023-06-05 16:41:07 -07:00
Nicolas Iooss 13e98fabc6 Format bytes with .hex() instead of :8X
Using `{buf:8X}` does not work in `LinuxNativeHelper.write_msr`: this
raises an expection

    TypeError: unsupported format string passed to bytes.__format__

Use `{buf.hex()}` instead to format the bytes in the error message.

Signed-off-by: Nicolas Iooss <nicolas.iooss_git@polytechnique.org>
2023-06-02 08:45:39 -07:00
Nathaniel Mitchell 745ae9084e Clean up HALs and Helpers
Signed-off-by: Nathaniel Mitchell <nathaniel.p.mitchell@intel.com>
2023-05-26 18:33:08 -07:00
Nathaniel Mitchell 89be1b4ad8 Remove native and add linuxnative as seperate helper
Signed-off-by: Nathaniel Mitchell <nathaniel.p.mitchell@intel.com>
2023-05-17 16:36:30 -07:00