Giter Site home page Giter Site logo

smoldtb's Introduction

A Tiny Flattened Device Tree Parser

Available Codeberg and Github.

This project is a standalone C port of the device parser from my kernel Northport. The original version has few limitations which are addressed here.

This version does make use of a single larger buffer for storing node data. This can either be allocated by a user provided function, or from a statically allocated buffer inside the program executable. The second method is suitable for environments where dynamic memory allocation might not be available, but it does limit the maximum number of nodes the parser can process.

Usage

Copy smoldtb.c and smoldtb.h into your project and you're good to go. No additional compiler flags are required.

The parser must be initialized before using it by calling dtb_init(). This function is the only time memory allocation/deallocation happens. You can call this multiple times, and it will re-initialize itself based on the new data device blob. Re-initializing the parser will destroy the previous parse data, so it effectively operates like a singleton.

The parser assumes that the DTB is always available at it's original address (the one given to dtb_init()) at runtime. If the DTB is moved in memory you can re-initialize the parser with the new address. The arguments for dtb_init(uintptr_t start, dtb_ops ops) are as follows:

  • uintptr_t start: The address where the beginning of the flattened device tree can be found. This should be where the FDT header begins and contain the magic number.
  • dtb_ops: a struct containing a number of function pointers to the library may need to call at runtime. Best practice is to populate all of these.

The dtb_ops struct has the following fields:

  • void* (*malloc)(size_t length): This function is called to allocate the buffer used internally by the parser. This is called once per call to dtb_init(). It should return a pointer to a region of memory free for use by the library that is at least length bytes in length. This function (and ops.free()) are both unused if using a statically allocated buffer.
  • void* (*free)(void* ptr, size_t length): Frees a buffer previously allocated by the above function. Only called when reinitializing the parser.
  • void (*on_error)(const char* why): If the library encounters a fatal error and cannot continue it will call this function with a string describing what happened and why.

Use Without Malloc/Free

Define SMOLDTB_STATIC_BUFFER_SIZE=your_buffer_size when compiling smoldtb.c and the parser will only allocate from a single buffer, typically stored in the program's .bss section. When compiled with this option ops.free() and ops.malloc() are never called.

In the event of parsing a DTB that contains too many nodes and/or properties for the static buffer, the parser will exit during dtb_init() (with a call to ops.on_error() if populated).

Concurrency

Not an advertised feature, but all (except dtb_init()) API functions will only read the internal structures and DTB. To be safe you may want to use a reader-writer lock around the library (only calls to dtb_init() will need the writer lock). If you only plan to initialize the parser once, even this is not necessary.

Changelog

v0.2.0

  • Renamed dtb_get_prop to dtb_find_prop.
  • Added a new dtb_get_prop which returns a property based on an index, rather than name.
  • Added support for using a static buffer instead of malloc/free.
  • Better documentation.

v0.1.0

  • Initial release.

smoldtb's People

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.