<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ville Baillie on Medium]]></title>
        <description><![CDATA[Stories by Ville Baillie on Medium]]></description>
        <link>https://medium.com/@villebaillie25?source=rss-b693aff00550------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*469icKFF1ilAp-vdPs4U8g.jpeg</url>
            <title>Stories by Ville Baillie on Medium</title>
            <link>https://medium.com/@villebaillie25?source=rss-b693aff00550------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 17:31:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@villebaillie25/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Revision Control of your Embedded Linux System]]></title>
            <link>https://medium.com/@villebaillie25/revision-control-of-your-embedded-linux-system-34bf4d5c7979?source=rss-b693aff00550------2</link>
            <guid isPermaLink="false">https://medium.com/p/34bf4d5c7979</guid>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[embedded-linux]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[linux-tutorial]]></category>
            <dc:creator><![CDATA[Ville Baillie]]></dc:creator>
            <pubDate>Sun, 14 Apr 2019 05:29:52 GMT</pubDate>
            <atom:updated>2019-04-14T05:29:52.119Z</atom:updated>
            <content:encoded><![CDATA[<p>Using Yocto you’ll need to juggle several repositories at once. You’ll have your ‘poky’ repository (typically you’ll get this from <a href="https://git.yoctoproject.org/git/poky">https://git.yoctoproject.org/git/poky</a>), then you’ll have your OpenEmbedded layer (usually retrieved from <a href="https://github.com/openembedded/meta-openembedded">https://github.com/openembedded/meta-openembedded</a>).</p><p>In addition, you’ll also have your chip specific layers, and then you’ll probably have your own project specific layer. So you may have anywhere up to 10 or possibly more repositories to handle.</p><p>Crafting ‘repo’ manifests</p><p>Earlier in this series, a short introduction to the tool named ‘repo’ was given. This tool allows multiple git repositories to be handled together as a cohesive whole. At its heart is the idea of a manifest, which is simply a list of git repositories and commit IDs (or tags or branches), and how to arrange them on your local filesystem.</p><p>Each manifest file is written in XML, so you would have your first line as typical XML version boilerplate:</p><p><strong>&lt;?xml version=”1.0&quot; encoding=”UTF-8&quot;?&gt;</strong></p><p>The entirety of the rest of the file should be held in a root element called “manifest”, so your next line would be</p><p><strong>&lt;manifest&gt;</strong></p><p>And the last line will always be</p><p><strong>&lt;/manifest&gt;</strong></p><p>Inside this root note you will enter all your remote servers in the form:<br> <br><strong>&lt;remote fetch=”remote-url” name=”remote-name”&gt;</strong></p><p>And you will enter all your repositories in the form:<br> <br><strong>&lt;project remote=”remote-name” revision=”commit-ID” name=”project-name”/&gt;</strong></p><p>The final fetch URL will be a combination of the “remote-url” and the “project-name”.<br>Below is an example of a manifest.xml</p><p><strong>&lt;?xml version=”1.0&quot; encoding=”UTF-8&quot;?&gt;<br>&lt;manifest&gt;</strong></p><p><strong>&lt;! — remotes →<br> &lt;remote fetch=”git://git.yoctoproject.org” name=”yocto”/&gt;<br> &lt;remote fetch=”git://git.openembedded.org” name=”oe”/&gt;<br> &lt;remote fetch=”git://github.com/OSSystems” name=”OSSystems”/&gt;<br> &lt;remote fetch=”git://github.com/meta-qt5&quot; name=”QT5&quot;/&gt;<br> &lt;remote fetch=”git://github.com/varigit” name=”variscite”/&gt;”<br> &lt;remote fetch=”ssh://git@gitserver/” name=”gitserver”/&gt;</strong></p><p><strong>&lt;! — upstream OE layers →<br> &lt;project remote=”yocto” revision=”dd0ba9ea4a11ab15348d4fe3574e4b28784db82f” name=”poky”/&gt;<br> &lt;project remote=”oe” revision=”ad6133a2e95f4b83b6b3ea413598e2cd5fb3fd90&quot; name=”meta-openembedded”/&gt;<br> &lt;project remote=”OSSystems” revision=”b6d46d69a261fe6bd7c1e9811dc2a9bbd0b79aeb” name=”meta-browser”/&gt;<br> &lt;project remote=”QT5&quot; revision=”d5536e34ec985c82b621448ab4325e5cbba38560&quot; name=”meta-qt5&quot;/&gt;</strong></p><p><strong>&lt;! — SoC vendor layers →<br> &lt;project remote=”gitserver” revision=”35b8b9bd9863de208ab60e33b55f10ee43e2619b” name=”NXP/meta-fsl-arm”/&gt;<br> &lt;project remote=”gitserver” revision=”e200df91b70da254461c59082ddd5db0a3c415a2&quot; name=”NXP/meta-fsl-arm-extra”/&gt;<br> &lt;project remote=”gitserver” revision=”2231e946e7a94d096394f2b2477e8184c9bbde7b” name=”NXP/meta-fsl-demos”/&gt;</strong></p><p><strong>&lt;! — SoM vendor layers →<br> &lt;project remote=”variscite” name=”meta-variscite-mx6&quot;/&gt;</strong></p><p><strong>&lt;! — Project specific layers →<br> &lt;project remote=”gitserver” revision=”master” name=”my-projects/project-X”/&gt;</strong></p><p><strong>&lt;/manifest&gt;</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/831/1*SXcdx4Lky_EvdSgWbFOlJg.png" /></figure><p>There are a few things to notice here. First is that I’ve got not only the SoC (system-on-chip) layers here (from NXP), but I’ve also added the SoM (system-on-module) layers as well (in this case from Variscite). You may or may not be using a SoM in your design, but if you are it generally means an extra layer or two.</p><p>The other thing to notice is that for the project specific layer, I’ve given ‘master’ as the revision. This means that ‘repo’ will checkout the master branch on that repository. This makes sense for development as we will probably be adding to that repository as we go whereas the other ones will be static.</p><p>The revisions chosen for all the other repositories should match in Yocto version, they should have been designed for a specific use case. As many of these as possible should be copied out of a manifest XML file for a reference design, if one exists.</p><p>N.b: a detailed description of repo manifest files can be found here <br><a href="https://gerrit.googlesource.com/git-repo/+/master/docs/manifest-format.txt">https://gerrit.googlesource.com/git-repo/+/master/docs/manifest-format.txt</a></p><p><strong>Basic repo usage</strong></p><p>You need to create a repository to hold your manifest file (I know another repository) is the last thing you need. But this can be on your local machine if you wish, at least to begin with.</p><p>Once this is done, you should find a folder in which you wish to keep all your sources, and run the following<br> <br><strong>repo init -u &lt;URL-of-manifest-repo&gt; -m &lt;manifest-filename-within-repo&gt; -b &lt;repo-branch-name&gt;</strong></p><p>This sets up the current directory as the working directory for this repo manifest, now you can run</p><p><strong>repo sync</strong></p><p>which will fetch all the repositories into the local directory. If you want to see if everything is as it should be (no changes in the repositories and branches with respect to their upstream brothers) you can run<br> <br><strong>repo status</strong></p><p>and if you want to update everything to the manifest discarding all changes you can run<br> <br><strong>repo sync –d</strong></p><p>these tools should give you enough get going with repo and <a href="https://www.bytesnap.com/">Embedded Linux development</a>.</p><p>And that concludes our series on How to Survive Embedded Linux.</p><p>We hope it’s been of use to you. There are occasions where it’s worth getting programming specialists to look into Embedded Linux issues for you.</p><p>At <a href="https://www.bytesnap.com/">ByteSnap</a>, we have several man-years of experience working on Linux BSP design, across a wide range of technologies and issues, which allows us to be adept at solving Linux conundrums.</p><p>We can also provide off-site specially tailored training on all matters Linux BSP — so get in touch to see how we can help with your Linux development project.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=34bf4d5c7979" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Debug your Linux Kernel]]></title>
            <link>https://medium.com/@villebaillie25/how-to-debug-your-linux-kernel-570399f36acc?source=rss-b693aff00550------2</link>
            <guid isPermaLink="false">https://medium.com/p/570399f36acc</guid>
            <category><![CDATA[embedded-linux]]></category>
            <category><![CDATA[linux-kernel]]></category>
            <category><![CDATA[linux-tutorial]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Ville Baillie]]></dc:creator>
            <pubDate>Sun, 14 Apr 2019 04:23:33 GMT</pubDate>
            <atom:updated>2019-04-14T04:23:33.543Z</atom:updated>
            <content:encoded><![CDATA[<p>In the previous installment of How to Survive Embedded Linux - <a href="https://medium.com/@villebaillie25/how-to-debug-your-linux-bsp-fd8bcb929544">How to Debug your Linux BSP</a> - we worked through some issues you might face trying to get your Linux BSP to boot for the first time. This touched on some kernel debugging. In this part of the series, we’re going to take those skills a little further.</p><p><strong>Debugging by printk</strong></p><p>By far the simplest and most commonly used debug method is the humble print statement. The Linux kernel offers this in the form of printk. The format style of printk is equivalent to its standard C cousin printf, and the main difference is the addition of log levels. For example:<br> <br><strong>printk(KERN_ALERT “reached line %d in function %s\n”, __LINE__, __func__);</strong></p><p>This line tell you what line and what function has been reached. You can litter these statements in the vicinity of where you think a crash is occurring, and start to claw back more information. Note the use of KERN_ALERT as the log level, when adding in spurious printk’s that will be removed later, I tend to use this log level as in most cases the print will make it to every console a user is logged into (see the kernel variable of console_loglevel).</p><p>Obviously, debugging by printk has its limitations, you need to recompile and reboot every time you want to debug a new section of code. Another issue is that prints are relatively resource intensive, and their very presence can cause errors to magically disappear, especially if they are related to timing/resource-contention.</p><p>One last thing: make sure to remove any printk statements after your debug has finished.</p><p><strong>How to analyse a crashdump</strong></p><p>When the Linux kernel crashes, also known as a ‘Kernel Panic’, you will get a printout similar to what’s below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/950/1*9XiAYh4UVu3hI41Yr9oFkQ.png" /></figure><p>If you haven’t seen many of these before, or even if you have, this can be very confusing to look at. Basically, what is being shown here are some prints from the crash itself describing the problem somehow (if you’re lucky). In this case, it says “Unable to handle kernel NULL pointer dereference at virtual address 00000000”.</p><p>There are many lines that give you register and stack contents. You generally won’t need to use these in 99% of cases. What will be most interesting is the list of function calls near the bottom. From the one in which the crash occurred to the top, to the earliest function call at the bottom.</p><p>Depending on whether or not you have debug symbols enabled in the kernel (CONFIG_DEBUG_INFO), the stacktrace that is shown here will either be a nice list of functions or an opaque list of memory addresses. If you haven’t got debug symbols enabled, you can still consult your mapfile to match function names to addresses (System.map in top level of Linux build directory).</p><p>The kernel panic stacktrace is very useful; it will give you a step-by-step thread of execution to the point where the fatal error occurred. Now you can fill it with printk’s, and try to set it off again. When it does happen again, you should be able to learn a little more about why.</p><p><strong>How to follow the thread of execution</strong></p><p>Often you’ll find something isn’t going quite right after you make a certain read/write/ioctl into a certain driver, and, after a bit of reading around, you’ll be able to find where userspace and the driver meet. From here you might want to follow the chain of function calls down and keep track of what is going on.</p><p>Following the thread of execution generally takes a few steps. I tend to use debug prints to find out which functions are being called, which branches are being taken, this generally takes time and a lot of prints but you will make forward progress.</p><p>Sometimes, you’ll run into a function pointer, where it isn’t clear what function is behind the reference. When this happens, you can ask your print to dump out the content of the pointer, you can then match this with your System.map file.</p><p>Admittedly, this is a very roundabout way to find out how code is being executed compared to a traditional debugger, however this is often the easiest way when dealing with the Linux Kernel. Another possible way to do things is by using KGDB.</p><p><strong>Using KGDB</strong></p><p>KGDB is a kernel debugger for Linux. To make use of it, you need to first enable CONFIG_KGDB and CONFIG_KGDB_KDB in your kernel configuration.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/424/1*6H6sbqfuO9w8hRJIj_i-XA.png" /></figure><p>You then need to make sure you have a serial connection between your host and target machines. What I mean by this is, you will be running GDB from your host, which will talk the KGDB service running alongside the kernel on your target hardware, the hardware you are testing.</p><p>Next, you need to pass “kgdboc=ttyS0, 115200” and “kgdbwait” as kernel command line parameters. (This assumes that you are connected to serial port ttyS0 on your target at 115200 Baud rate. Change it to match your specific setup if necessary).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/428/1*HhyB2SOlS-9NUPC-srFHIA.png" /></figure><p>Then, during bootup, when KGDB is initialised, the ‘kgdbwait’ parameter will cause execution to pause and be passed to the kdb debugger.</p><p>From the kdb command prompt, you can perform various commands, for example you can call a backtrace with ‘bt’, or you can read registers with ‘dr’. If you’re using a multi-processor CPU, then you can change cores with ‘cpu’.<br>You can also connect gdb from your host computer for more advanced debugging. This allows you to step through the kernel code like a regular application (hence KGDB). This will allow you to set breakpoints, at which point you can continue</p><p>Also, you can enter into kdb/kgdb in various different ways and not just during boot up. For example, you can run “echo g &gt; /proc/sysrq-trigger” to access the GDB console. Just make sure that “kgdboc=ttyS0, 115200” is defined in your kernel command line parameters and “kgdbwait” isn’t.</p><p>Ville is a software engineer at ByteSnap Design, an award winning <a href="https://www.bytesnap.com/">software and hardware consultancy</a>. He is a graduate of Physics at Warwick University and now specialises in developing device drivers for Embedded Linux applications.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=570399f36acc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[In this part of the series on How to Survive Embedded Linux series focuses specifically on…]]></title>
            <link>https://medium.com/@villebaillie25/in-this-part-of-the-series-on-how-to-survive-embedded-linux-series-focuses-specifically-on-c30b4ba6c0b9?source=rss-b693aff00550------2</link>
            <guid isPermaLink="false">https://medium.com/p/c30b4ba6c0b9</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[embedded-linux]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[debugging]]></category>
            <category><![CDATA[linux-application]]></category>
            <dc:creator><![CDATA[Ville Baillie]]></dc:creator>
            <pubDate>Sat, 13 Apr 2019 10:52:16 GMT</pubDate>
            <atom:updated>2019-04-14T04:12:13.915Z</atom:updated>
            <content:encoded><![CDATA[<p>How to debug your Linux Application: Debugging by printf</p><p>In this part of the series on How to Survive Embedded Linux series focuses specifically on debugging C/C++ applications. Apps written in scripting languages like Perl or Python have their own debugging best-practices protocols and tools.</p><p>This is the simplest technique, directly analogous to ‘Debugging by printk’ in the previous article “<a href="https://medium.com/@villebaillie25/how-to-debug-your-linux-bsp-fd8bcb929544">How to Debug your Linux BSP</a>”. This method is the most commonly used by engineers who prefer not to use a debugger. The usual caveats apply, adding print statements can wildly affect timing, which can causes old bugs to vanish and new ones to appear.</p><p>Always remember to clean up your debug prints before shipping your code! An old piece of advice is, “never remove your debug prints”; if they were useful to add in once then they may be useful again. While this piece of advice becomes onerous if carried out all the time, it does lead us on to our next topic…</p><p><strong>Debugging by logging</strong></p><p>A log message is a bit like a debug print line that you leave inside the project. You add these in as you go.</p><p>Often, a large project will contain its own logging system. This will allow different levels of log messages to be used throughout the code, with some level of configurability determining what is done with those messages.<br>This doesn’t have to be very complicated to implement; generally, you’d create a function that looks something like the following:<br> <br><strong>int log_printf(enum log_level lvl, const char *fmt, …);</strong></p><p>Inside this function, there would typically be some logic to send the log message to different places given the debug level passed. I will highlight a few examples of different debug levels below:</p><p><strong>LOG_ERROR</strong></p><p>At the top of the logging food chain, would be the error message. Any time a major error occurs, this should be printed to the user on STDERR with a decent amount of information.</p><p><strong>LOG_WARNING</strong></p><p>A warning is just that, it might be an issue or it might not. Typically, this should be printed to the user on STDERR. Some information is good, but don’t overload the user too much.</p><p><strong>LOG_DEBUG</strong></p><p>This is the sort of message that can help the developer understand what’s going on. But by default, it probably shouldn’t even be getting compiled into the released source code. If it is compiled in, it should be getting sent to a file and not overloading the user with too much information.</p><p>These are not hard and fast rules, it very much depends on what your application is doing, how large it is, how it interacts with other systems and so-on. The general point though, is that logging and especially good error messages can save time. It will take slightly longer to write the code in the first place, but it is a good habit to get into, and when problems do show up it can help you to diagnose the problem immediately without any further debug effort required.</p><p><strong>Debugging by GDB</strong></p><p>The GNU project provides a command line debugging tool called GDB. This can be very helpful during development as, just like a regular debugger, it can help you see what’s going on inside the program. It can even be useful to find out where a segmentation fault occurred.</p><p>To begin using GDB, the application you are compiling with must have debug flags enabled. These are like little bookmarks throughout your compiled code, which link machine instructions back to the file and line it originally came from in your C sources. Simply add “-g” into your CFLAGS.</p><p>To run a program with gdb, simply run ‘gdb &lt;program-name&gt;’. As an example, here is a simple C file called “a.c”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/347/1*noXKvDESNfcAUgm6jYF3Fw.png" /></figure><p>I compile with “-g” and run it with gdb and get to a (gdb) prompt.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/567/1*PUhGCVFS9AfwzfHaxIYKQg.png" /></figure><p>From here I can run the program, set breakpoints, example values and do single steps.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/339/1*FMqBAWHFmLpNp6wuaj55_g.png" /></figure><p>In the above screenshot, I set breakpoints at lines 9, 17, and run the program. It first stops at line 19 (line 17 is only the declaration of a variable so execution will not stop here). From here, I examine the value of the variable ‘i’ which is shown to be zero.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/268/1*PHm7C9M7a3QhZka8r_Ip6A.png" /></figure><p>From here I go to the next breakpoint, which is inside my function call, I examine the values contained in ‘a’ and ‘b’, and we can see they contain four and two respectively.</p><p>This is just a small sample of how to use GDB. It is very powerful and there are many more things you can do with it. However, it is a little cumbersome to debug like this on the command line. GDB can either be used interactively — as we’ve seen — or it can be used as simply a backend for a much more user-friendly interface.</p><p><strong>Debugging by VisualGDB</strong></p><p>VisualGDB is a commercial software add-on to Microsoft Visual Studio. It’s a godsend for programmers who wish to develop in the Visual Studio environment they are used to, but also need the flexibility to target any sort of Linux platform.</p><p>Generally, to use VisualGDB you’ll want three separate machines.</p><ol><li>The Visual Studio host machine — this is where you’ll develop and debug your application.</li><li>The cross-compilation machine — this is where VisualGDB will farm out compilation of your application. Your target’s corresponding Yocto SDK should be installed here.</li><li>The target machine — this is where your app will be running and where GDB will be running as a backend.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lT_KF_X0Cun_wA4M4c05tA.png" /></figure><p>Once you have all this setup, you will be able to set breakpoints in various files with the mouse, and debug just like (or similar to) a native Windows application. For a lot of developers, this makes life much easier.</p><p><strong>Debugging with Valgrind</strong></p><p>One last tool I’d like to mention in this article is Valgrind. It’s actually a suite of tools, but the most important one is Memcheck.</p><p>This tool is useful for discovering memory leaks or out-of-bounds accesses, common issues in C/C++. To run it, first make sure your program was compiled with “-g” as described in the ‘Debugging with GDB’ section, then simply run your program with the following:</p><p><strong>valgrind — leak-check=yes &lt;your-program&gt; &lt;your-arguments&gt;</strong></p><p>Your program will run many times slower than normal; this is due to it running your program in a virtual environment, where in-depth real-time memory analysis can take place as it is being run.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/925/1*CNEs67FSgGdNXvFtPY57pg.png" /></figure><p>Depending on the complexity and structure of your program, Valgrind may or may not be able to provide you with any useful information. Not all boundary errors can be caught, however if you do find your application is leaking memory or seg-faulting, Valgrind can be a great zero-effort first attempt in tracking it down.</p><p>Debugging your Linux kernel requires a different approach to debugging your Linux application. We’ll take a closer look at that in the next part of How to Survive Embedded Linux.</p><p>Ville Baillie</p><p>Ville is a software engineer at ByteSnap Design, an award winning <a href="https://www.bytesnap.com/">software design company</a> and hardware consultancy. He is a graduate of Physics at Warwick University and now specialises in developing device drivers for Embedded Linux applications.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c30b4ba6c0b9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to debug your Linux BSP]]></title>
            <link>https://medium.com/@villebaillie25/how-to-debug-your-linux-bsp-fd8bcb929544?source=rss-b693aff00550------2</link>
            <guid isPermaLink="false">https://medium.com/p/fd8bcb929544</guid>
            <category><![CDATA[embedded-linux]]></category>
            <category><![CDATA[linux-bsp]]></category>
            <category><![CDATA[debugging]]></category>
            <category><![CDATA[linux]]></category>
            <dc:creator><![CDATA[Ville Baillie]]></dc:creator>
            <pubDate>Thu, 14 Mar 2019 15:27:31 GMT</pubDate>
            <atom:updated>2019-03-14T15:27:31.397Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/501/1*mEbnWffGFQi7T6kLwLqTdw.png" /></figure><p><strong>Getting to the U-Boot command line</strong></p><p>Getting to the U-Boot command line is fundamental, a whole host of things must be correct to get there. Clocks, memory, serial…: these are some of the things that need to be working before you talk to your board at all.</p><p>To debug quickly, try to create an environment where you can test successive U-Boot images with a fast turnaround. Debugging is the act of gaining more information, and at this stage the only tool you have is re-compiling, re-flashing (re-loading?), and re-running U-Boot. Create a setup that does all these things efficiently.</p><p>Keep notes of what you are doing, constantly going round the cycle of debug and not getting far can turn you stir crazy at times, and make you forget things you’ve seen. Let’s begin at the worst case:</p><p><strong>Nothing is printed on the serial port</strong></p><p>The first board arrives. You’ve tested that the power rails all come on OK. You connect the serial port, load U-Boot, cross your fingers and turn on the board. And… nothing, not a single character is printed.</p><p>The first thing to do is to try and turn on an LED. Find a section of code that is executed early, before the serial console is even brought up, and use it to turn on an LED (just use a raw memory access). If it works, great! If not then try moving it back through the execution chain as early as possible. If this doesn’t work, then U-Boot probably isn’t being loaded at all, and you need to consult your CPU documentation.</p><p>If you can turn on an LED, then maybe your serial port is not being configured correctly. Try turning your LED on after the serial port is configured, is it still turning on? Now you know your serial port initialisation isn’t crashing U-Boot entirely. Double check the serial port being used, double check rates, protocol, flow control. Get an oscilloscope or a logic analyser and test for activity on the lines.</p><p>If there is rubbish on the serial port, then perhaps there is an issue with the PLL configuration. Or the signal is inverted. Or you are just seeing noise from moving the connectors. Is the outputted signal transmitted as TTL or RS-232?<br>Once you have some sort of serial port communication established, things get a bit easier:</p><p><strong>Prints stop short of reaching the command line</strong></p><p>So there are prints occurring on the serial port, but it stops short of reaching the command prompt. Communication with your board is still a one-way street.</p><p>The only thing to do now is to determine why it stops there. The most important information is the last thing to be printed. By searching the source code, you should be able to find out where a certain string is printed. You will become adept at this skill through practise, which you will need as it is fundamental to debugging Linux BSPs (U-Boot and the Linux Kernel).</p><p>Once you have located the source of the last print, you will want to add more prints later in the execution chain. This will help you to gain more information about why the prints mysteriously stop working. The approach can almost always be used to reduce the possible location of the error to a single statement if required. It is time consuming, but this is why setting up your working environment to be efficient to this purpose is so important.</p><p>Eventually you will find the source of the issue, perhaps the memory is not being brought up correctly, or maybe a certain peripheral access causes a system wide crash. Be persistent, but remember that sometimes the mere act of adding prints to a system can cause timing changes that will artificially suppress an error. If you have time, always try to understand in full any failure modes that are observed, if you fix them without knowing why, they can sometimes come back to haunt you.</p><p><strong>Booting Linux</strong></p><p>So you’ve reached the command line. But booting Linux does not work as expected. At least now, you can talk to your processor, U-Boot has many useful commands that allow you to probe memory, read filesystems and even fetch files from a network. Some of these commands will be needed to boot Linux, and they may not work for various reasons.</p><p>A peripheral needed for boot is not working</p><p>To boot Linux, U-Boot will need to find various binaries (kernel, device-tree, filesystem) from somewhere. Generally, this somewhere is some sort of internal storage on the board, but it is just as valid to fetch things off the network.</p><p>U-Boot does things a little differently to Linux; for example, U-Boot’s driver support tends to lag a few months behind its bigger brother. Sometimes, your shiny new flash chip is simply not supported in the older version of U-Boot you are using, and you don’t quite realise this until boards are in house.</p><p>When this happens, there are a few things you can try. First, check the latest U-Boot upstream source code, things move fast in the open-source world, and you might find a nice new driver to backport. Second, you can try looking at the Linux source code, a driver found here will be a little harder to backport, and it depends on which subsystem the driver is a part of. Some will be easy, some will be next to impossible.</p><p>If all else fails, try scouring the internet, it might be the case that someone has submitted a driver for the chip in question, it just hasn’t been accepted and merged in yet. Sometimes this process can take months and then stop simply due to lack of interest on both parties; I have found patches years old that have proven to be useful for my purposes.</p><p><strong>U-Boot claims to boot Linux but nothing happens</strong></p><p>So, you see “Starting Kernel …” on the serial port from U-Boot, but then nothing happens. Another frustrating problem and it can feel like we’re back where we started with nothing printing on the serial port. To make things worse, we’re now blurring the line between debugging the U-Boot world and debugging the Linux world.</p><p>Typically, if there was obviously wrong with the images you have provided, U-Boot will complain noisily, and you will be able to debug the problem. What’s likely happened here is that Linux is booting, but it is not printing to the correct serial console. Double check the boot arguments in U-Boot (sometimes you need to set “console=ttyS0” or equivalent), double-check the kernel configuration you have used to build your kernel. If you have Ethernet enabled, plug in an Ethernet cable and see if the link lights come on, if they do then Linux is booting flawlessly and is only failing to print to the serial console.</p><p>Another thing that could be wrong is the LOADADDR specified was wrong. Some setups require you to specify this environmental variable when compiling the Linux kernel. If this is wrong, then U-Boot will quietly pass control to an improperly linked kernel, and the CPU will quickly fall over.<br>You may need to enable early printing, how you do this is highly dependent on your processor. On ARM you would enable CONFIG_DEBUG_LL. You’ll need to tell the configuration how to access the serial port system, and which one to use. This allow you to use the “early_print” function which should give you prints before the console is enabled. Try adding them into the start_kernel() function found in “init/main.c”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/501/1*mEbnWffGFQi7T6kLwLqTdw.png" /></figure><h4><strong>Linux forever “waiting for root device”</strong></h4><p>Linux begins booting, you see a whole stream of boot log messages, but then it hangs forever on “Waiting for root device”. The first thing you’ll want to do is check your boot arguments in U-Boot, in a lot of cases, Linux requires U-Boot to tell it which device contains the root partition. It takes some skill and domain knowledge to know what the device will be called, but you can generally work it out either from first principles or trial and error.</p><p>For example, if you are booting from eMMC on an iMX6 device, which is attached to MMC port 4 in the hardware manual, and the root partition is number 1, then the boot arguments should contain “root=/dev/mmcblk3p1”. The way of working this out will be different dependent on your design and CPU architecture.</p><p><strong>Any other boot problems</strong></p><p>If there is any other sort of failure during the boot process, then try to analyse where the error is occurring. It could be a driver initialisation that fails and crashes the whole system. If you are really stuck, remove the driver from the kernel configuration entirely, returning to it at a later date.<br>The crash could also be occurring in the initscripts, which means your problem is now a combination of user space and kernel space. Try and determine which initscript (typically these are the scripts in /etc/init.d) is being executed as the crash happens.</p><p><strong>Debugging drivers after boot</strong></p><p>Once Linux is booting fully there is ordinarily still more work to be done getting all your drivers to work. You’ll want to use the same process of re-compile, re-run to find out why something isn’t working. Some drivers you can build as modules, which means you can re-run without having to reboot the board. Use this to your advantage and speed up your debug time.</p><p><strong>Asking for help</strong></p><p>Sometimes when you have a particularly difficult problem in your Linux BSP that you can’t solve, simply taking a break, or sleeping on it, can be enough to get you going again. Sometimes however, you will need to get help, and there are several ways you can go about this. You could ask a colleague in your organisation to look at it with you; often a second pair of eyes can solve these types of issues. Otherwise, you can look for support online: your CPU manufacturer will most likely have public forums where you can ask questions related to Linux BSP issues, if you think your problem is with the software more than with the chip, you could write an enquiry to the relevant mailing list.</p><p>If all else fails it might be worth hiring an expert to look into the issue for you. Here at ByteSnap we have many years expertise working on Linux BSP design, across a wide range of technologies and issues. Often problems can arise again and again in similar ways, and the benefit of experience allows us to identify and quickly solve these. We can also provide off-site specially tailored training on all matters Linux BSP.</p><p>Ville is a software engineer at ByteSnap Design, an award winning software and hardware consultancy. He is a graduate of Physics at Warwick University and now specialises in developing device drivers for <a href="https://www.bytesnap.com/software-development/">Embedded Linux</a> applications.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fd8bcb929544" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Survive Embedded Linux: How to Compile]]></title>
            <link>https://medium.com/@villebaillie25/how-to-survive-embedded-linux-how-to-compile-62399700d50c?source=rss-b693aff00550------2</link>
            <guid isPermaLink="false">https://medium.com/p/62399700d50c</guid>
            <category><![CDATA[embedded-linux]]></category>
            <category><![CDATA[embedded]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[embedded-systems]]></category>
            <dc:creator><![CDATA[Ville Baillie]]></dc:creator>
            <pubDate>Tue, 05 Mar 2019 08:46:34 GMT</pubDate>
            <atom:updated>2019-04-04T13:50:36.002Z</atom:updated>
            <content:encoded><![CDATA[<p>One of the first challenges in designing a Linux BSP and associated software is just how to compile the thing in the first place.<br> <br>In this part of the series on How to Survive Embedded Linux, we’ll guide you through some best practice ideas and techniques to utilise when learning how to compile your Linux BSP.</p><p>How to Compile</p><p><strong>Cross-compilation: an introduction</strong><br>A “cross-compiler” is a compiler that creates executable code for a different architecture. In the case of developing your Linux BSP, you’ll be compiling on your PC but the code you make is targeting your custom board.</p><p>Things that need to be compiled include, but are not limited to: the bootloader, the kernel, shared libraries, your application code. We’ll look at a few ways of getting this done</p><p><strong>Finding the right cross-compiler</strong><br>To get your cross-compiler you will need to find it from somewhere. There are several places where you might find what you need, but for a newbie some of the options and terminology might be a bit confusing.</p><p>If you’re using Ubuntu and you’re looking for an ARM cross-compiler based on GCC, you might find the following packages that may suit your needs: -</p><p>gcc-arm-linux-androideabi gcc-arm-linux-gnueabi gcc-arm-linux-gnueabihf gcc-arm-none-eabi gcc-arm-none-eabi-source</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/616/1*MOcJDM6sH64pg8IlFq1Ttg.png" /></figure><p>In this scenario you’ll want to pick out the one that includes linux-gnueabi. The ABI, or application binary interface, is different if you’re compiling for Android (linux-androideabi) or GNU/Linux (linux-gnueabi). The gcc-arm-none-eabi is for bare-metal programming, when you want to avoid an operating system altogether.</p><p>The reason for the difference is due to the multiple C implementations that exist for different operating systems, as we are compiling for Linux and using the GNU C Library (generally what is understood by a ‘Linux BSP’) then we must use the gcc-arm-linux-gnueabi cross-compiler.</p><p>One last point, there is a gcc-arm-linux-gnueabihf compiler there to, so what is the difference there? In this case the ‘hf’ stands for hard float, in other words, you should choose this compiler if your target includes a hardware floating-point unit, this will considerably speed up operations that include floating point calculations.</p><p>Remember that, if you do decide to use your package manager, there may be updates to your compiler over time as your package manager does periodic updates. This will stop your builds being reproducible over time. In a production setting, you’re more likely to want to pick a specific compiler and stick with it as long as you can.</p><p>In most cases the CPU vendor will provide, or point to, a toolchain that is suitable and has been well tested. Use this unless you have reason to move to another one, different versions of GCC can have different compile bugs, or even different behaviour; when making a switch, make sure to understand the differences and carry out a good set of tests.</p><p><strong>Using ‘sysroots’</strong></p><p>A sysroot is a scaled down version of your target’s filesystem, it need only contain the libraries and headers which you will compile/link against. There are many ways to set up a sysroot, one is to copy the /usr and /lib directories from your target to somewhere on your host filesystem. You could also NFS (network file system) mount the target filesystem somewhere locally.</p><p>The GCC family of compilers accept a “ — sysroot=dir” argument, which is used to specify the path to your sysroot. Check the documentation for your cross-compiler to how to set your compiler correctly, you may need to take extra steps for using the sysroot when passing include/library directories.</p><p>If you compiler does not support using a sysroot then you can always just tell the compiler to look in your sysroot manually, if you’re using a makefile then it’s not extra effort once everything is setup. Just try to keep your sysroot unchanged during development.</p><p><strong>Cross compilation with autotools<br></strong>Most well-known Linux software uses autotools, it’s likely you’ve used its famous two step build process (./configure, make) when compiling a package from source. Moving your project to use autotools can take a great amount of effort, but its benefits include very straight-forward cross compilation.<br>To cross-compile with autotools you need only change the flags passed to configure. In the example that you are building for your local machine (i.e.: not cross-compiling), you run configure without any flags. When cross-compiling, there are four flags you will want to use:-</p><p>- -build=&lt;build&gt; this is the machine you are building on (e.g.: x86_64-linux)<br>- -target=&lt;target&gt; the machine you are building for (e.g.: arm-linux-gnueabihf)<br>- -with-sysroot=&lt;sysroot&gt; the sysroot as described in the previous section</p><p>After the configure script is done, you can run ‘make’ as normal and the cross-compiler will be automatically selected and passed the correct flags. If you encounter any errors in the configure stage of the process, just go through one by one and try to fill any missing gaps.</p><p><strong>Cross compilation with Yocto</strong><br>If you’ve been using Yocto to build your Linux BSP, then consider building a Yocto SDK to make your life much easier going forward.</p><p>To build an SDK run the following bitbake command:-</p><blockquote>bitbake &lt;your-image-name-here&gt; -c do_populate_sdk</blockquote><p>This will create an installer in your “build/tmp/deploy/sdk” directory. Refer to the Yocto manual for more details. Install your SDK somewhere on your filesystem.</p><p>Once it is installed there is a script that begins with “environment-setup-” followed by the target machine that the SDK builds for. This script will export a series of very useful variables into your environment, allowing you to cross-compile using tools actually generated by Yocto itself.</p><p>A few example variables are given below for an ARM Cortex A9 processor target:-</p><p>export SDKTARGETSYSROOT=/home/villeb/sdk/sysroots/cortexa9hf-vfp-neon-poky-linux-gnueabi</p><p>export CFLAGS=” -O2 -pipe -g -feliminate-unused-debug-types”</p><p>export CC=”arm-poky-linux-gnueabi-gcc -march=armv7-a -mfloat-abi=hard -mfpu=neon -mtune=cortex-a9 — sysroot=$SDKTARGETSYSROOT”</p><p>export CXX=”arm-poky-linux-gnueabi-g++ -march=armv7-a -mfloat-abi=hard -mfpu=neon -mtune=cortex-a9 — sysroot=$SDKTARGETSYSROOT”</p><p>export CONFIGURE_FLAGS=” — target=arm-poky-linux-gnueabi — host=arm-poky-linux-gnueabi — build=i686-linux — with-libtool-sysroot=$SDKTARGETSYSROOT”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/735/1*QuyfCr-w8tCH3YjN8wzDZA.png" /></figure><p>As you can see, it will give you sensible compiler defaults and flags to use for cross-compiling. It will also give you a full set of flags to pass to an autotools configure script, so all you have to do is run ./configure ${CONFIGURE_FLAGS} and you’re cross-compiling with a Yocto built binary.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/566/1*CNtqUIC7ev0XN5vw0We9Wg.png" /></figure><p><strong>Conclusion</strong></p><p>Here we have covered the most straightforward methods with the most straightforward terminology. There are other methods, for example “chroot cross-compilation”, which we have not gone into as I believe these contain unneeded complexity or only address specific target/host combinations. Someone might quiz you on the meaning of “Canadian-cross-compilation”, this term only applies to cross-compiling a cross-compiler, and you won’t need it for 99.9% percent of your daily work.</p><p>Ville is a software engineer at ByteSnap Design, an award winning software and <a href="https://www.bytesnap.com/">embedded Linux development company</a>. He is a graduate of Physics at Warwick University and now specialises in developing device drivers for Embedded Linux applications.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=62399700d50c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>