Skip to content

Commit

Permalink
DATA_DIR override with environment variable (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
trabucayre committed Apr 17, 2023
1 parent 1bdb117 commit cd8a143
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ set(OPENFPGALOADER_SOURCE
src/anlogicBitParser.cpp
src/anlogicCable.cpp
src/ch552_jtag.cpp
src/common.cpp
src/dfu.cpp
src/dfuFileParser.cpp
src/dirtyJtag.cpp
Expand Down Expand Up @@ -140,6 +141,7 @@ set(OPENFPGALOADER_HEADERS
src/anlogicBitParser.hpp
src/anlogicCable.hpp
src/ch552_jtag.hpp
src/common.hpp
src/cxxopts.hpp
src/dfu.hpp
src/dfuFileParser.hpp
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,21 @@ for any corresponding short options.
Report bugs to <gwenhael.goavec-merou@trabucayre.com>.
```

By default **spiOverJtag** are search into `${CMAKE_INSTALL_FULL_DATAROOTDIR}`
(*/usr/local/share/* by default). It's possible to change this behaviour by
using an environment variable:

```bash
export OPENFPGALOADER_SOJ_DIR=/somewhere
openFPGALoader xxxx
```

or

```
OPENFPGALOADER_SOJ_DIR=/somewhere openFPGALoader xxxx
```

`OPENFPGALOADER_SOJ_DIR` must point to directory containing **spiOverJtag**
bitstreams.
17 changes: 17 additions & 0 deletions doc/guide/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,20 @@ Writing to an arbitrary address in flash memory
With FPGA using an external SPI flash (*xilinx*, *lattice ECP5/nexus/ice40*, *anlogic*, *efinix*) option ``-o`` allows
one to write raw binary file to an arbitrary adress in FLASH.
Using an alternative directory for *spiOverJtag*
================================================
By setting ``OPENFPGALOADER_SOJ_DIR`` it's possible to override default
*spiOverJtag* bitstreams directory:
.. code-block:: bash
export OPENFPGALOADER_SOJ_DIR=/somewhere
openFPGALoader xxxx
or
.. code-block:: bash
OPENFPGALOADER_SOJ_DIR=/somewhere openFPGALoader xxxx
8 changes: 4 additions & 4 deletions src/altera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <string>

#include "common.hpp"
#include "jtag.hpp"
#include "device.hpp"
#include "epcq.hpp"
Expand Down Expand Up @@ -181,12 +182,11 @@ bool Altera::load_bridge()
return false;
}

// DATA_DIR is defined at compile time.
bitname = DATA_DIR "/openFPGALoader/spiOverJtag_";
bitname = get_shell_env_var("OPENFPGALOADER_SOJ_DIR", DATA_DIR "/openFPGALoader");
#ifdef HAS_ZLIB
bitname += _device_package + ".rbf.gz";
bitname += "/spiOverJtag_" + _device_package + ".rbf.gz";
#else
bitname += _device_package + ".rbf";
bitname += "/spiOverJtag_" + _device_package + ".rbf";
#endif
}

Expand Down
21 changes: 21 additions & 0 deletions src/common.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: Apache-2.0
/*
* Copyright (C) 2023 Gwenhael Goavec-Merou <gwenhael.goavec-merou@trabucayre.com>
*/


#include "common.hpp"

#include <string>
#include <cstdlib>

/*!
* \brief return shell environment variable value
* \param[in] key: variable name
* \return variable value or ""
*/
const std::string get_shell_env_var(const char* key,
const char *def_val) noexcept {
const char* ret = std::getenv(key);
return std::string(ret ? ret : def_val);
}
20 changes: 20 additions & 0 deletions src/common.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: Apache-2.0
/*
* Copyright (C) 2023 Gwenhael Goavec-Merou <gwenhael.goavec-merou@trabucayre.com>
*/

#ifndef SRC_COMMON_HPP_
#define SRC_COMMON_HPP_

#include <string>

/*!
* \brief return shell environment variable value
* \param[in] key: variable name
* \param[in] def_val: value to return when not found
* \return variable value or def_val
*/
const std::string get_shell_env_var(const char* key,
const char *def_val="") noexcept;

#endif // SRC_COMMON_HPP_
6 changes: 3 additions & 3 deletions src/xilinx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "jtag.hpp"
#include "bitparser.hpp"
#include "common.hpp"
#include "configBitstreamParser.hpp"
#include "jedParser.hpp"
#include "mcsParser.hpp"
Expand Down Expand Up @@ -468,9 +469,8 @@ bool Xilinx::load_bridge()
return false;
}

// DATA_DIR is defined at compile time.
bitname = DATA_DIR "/openFPGALoader/spiOverJtag_";
bitname += _device_package + ".bit.gz";
bitname = get_shell_env_var("OPENFPGALOADER_SOJ_DIR", DATA_DIR "/openFPGALoader");
bitname += "/spiOverJtag_" + _device_package + ".bit.gz";
}

#if defined (_WIN64) || defined (_WIN32)
Expand Down

0 comments on commit cd8a143

Please sign in to comment.