Skip to content

Commit

Permalink
[core] Add GNU Build ID as firmware identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Jun 6, 2019
1 parent 4d4af70 commit 729ae1f
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 1 deletion.
29 changes: 29 additions & 0 deletions src/modm/architecture/interface/build_id.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2019, Niklas Hauser
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#pragma once

#include <cstdint>
#include <array>

namespace modm
{

/**
* Return the GNU Build ID as a 160-bit SHA1 array.
*
* @ingroup modm_architecture_build_id
*/
[[nodiscard]]
const std::array<uint8_t, 20>&
build_id();

}
14 changes: 14 additions & 0 deletions src/modm/architecture/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ class BlockDevice(Module):
env.copy("interface/block_device.hpp")
# -----------------------------------------------------------------------------

class BuildId(Module):
def init(self, module):
module.name = "build_id"
module.description = "GNU Build ID"

def prepare(self, module, options):
return True

def build(self, env):
env.outbasepath = "modm/src/modm/architecture"
env.copy("interface/build_id.hpp")
# -----------------------------------------------------------------------------

class Can(Module):
def init(self, module):
module.name = "can"
Expand Down Expand Up @@ -348,6 +361,7 @@ def prepare(module, options):
module.add_submodule(Assert())
module.add_submodule(Atomic())
module.add_submodule(BlockDevice())
module.add_submodule(BuildId())
module.add_submodule(Can())
module.add_submodule(Clock())
module.add_submodule(Delay())
Expand Down
33 changes: 33 additions & 0 deletions src/modm/platform/core/cortex/build_id.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2019, Niklas Hauser
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#include <modm/architecture/interface/build_id.hpp>

typedef struct {
uint32_t namesz;
uint32_t descsz;
uint32_t type;
uint8_t data[];
} ElfNoteSection_t;
extern "C" const ElfNoteSection_t __build_id[];

namespace modm
{

const std::array<uint8_t, 20>&
build_id()
{
const uint8_t *const sha1 = &__build_id->data[__build_id->namesz];
auto *const hash = reinterpret_cast<const std::array<uint8_t, 20> *>(sha1);
return *hash;
}

}
6 changes: 6 additions & 0 deletions src/modm/platform/core/cortex/linker.macros
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ TOTAL_STACK_SIZE = MAIN_STACK_SIZE + PROCESS_STACK_SIZE;
KEEP(*(.assertion))
__assertion_table_end = .;
} >{{memory}}

.build_id :
{
__build_id = .;
KEEP(*(.note.gnu.build-id))
} > {{memory}}
%% endmacro


Expand Down
4 changes: 4 additions & 0 deletions src/modm/platform/core/cortex/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def prepare(module, options):
":architecture:interrupt",
":architecture:memory",
":architecture:unaligned",
":architecture:build_id",
":platform:clock",
":cmsis:device")

Expand Down Expand Up @@ -274,6 +275,9 @@ def build(env):
env.template("delay.cpp.in")
env.copy("delay.hpp")

# GNU Build ID
env.copy("build_id.cpp")

# modm-test implements the clock methods itself
if not env.has_module(":test:architecture"):
env.copy("clock.cpp")
Expand Down
3 changes: 2 additions & 1 deletion tools/build_script_generator/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ def common_compiler_flags(compiler, target):
"-Wl,--fatal-warnings",
"-Wl,--gc-sections",
"-Wl,--relax",
"-Wl,-Map,{target_base}.map,--cref",
"-Wl,--build-id=sha1",
# "-Wl,-Map,{target_base}.map,--cref",
]
# C Preprocessor defines
flags["cppdefines"] = []
Expand Down

0 comments on commit 729ae1f

Please sign in to comment.