raspberry pi : writing Linux device driver - linux-kernel

I have some basics knowledge on Linux (RHEL 5.4) Device Driver and Kernel internals and wish to gain expertise on same. I came to know of raspberry pi board.
My question is that the same code that I write on a Linux server will work there - is their architecture and concepts same. Kindly note that if it is not the same case then I need to buy a desktop PC otherwise for offline practicing purpose.
Note - I was unable to add raspberry pi group hence needed to remove the same and add the below ones.

Yes it depends on Architecture and the same code compiled on x86 will not wrok on Pi. However, there are ways to get around it.
As mentioned in the above post, use a cross compile toolchain(that comes with its own libc) to comile your code (kernel/userspace) to try it out on R pi. Again doing this, you will still not be able to test your code. To do that get a VM tool like qemu. I am not sure if there is a qemu port for R pi but in general a ARM 11 (ARMv6) based qemu should do. The following link should get you going with initial kernel development on your PC without owning a R pi.
http://xecdesign.com/qemu-emulating-raspberry-pi-the-easy-way/
Cheers
Subbu

Is their architecture and concepts same??
I would like to clarify that Rasperry Pi is ARM based board. Mostly I guess your server is running on X86.
Device drivers meant for devices. Rasperry Pi should have the device which you are writing driver for.
I suggest you to study the data sheet of rasperry pi and linux driver model.
Linux driver model is architexture independent only. so you need only some effort for porting your X86 driver to ARM. You need to concentrate on hardware part.

You might need to cross compile your code for ARM arch. if you are using x86 machine on your Linux Server.You can cross compile your modules for ARM using GNU ARM toolchain and then run on Raspberry pi.

Related

setting up a development environment for Linux device driver

I am trying to read the LDD book by Jonathan Corbet, Greg Kroah-Hartman, Alessandro Rubini and implement the sample modules. So to begin with, I tried setting up a development system. Installed Ubuntu 16.04 Xenial. Now, I just created a directory and wrote the hello_world module with a Makefile. Got it built and run it, verified the dmesg logs.
Is that all the development setup? I searched online and found articles where they are asking to download and compile the kernel, use a VM to boot the kernel. What is the reason? Or what am I missing?
Is there any better article which clarifies this?
Thanks
hago
You can try one more way:
If you have native windows, install virtual machine software such as
Virtual box. Get your favourite Linux distribution (no bias, just
an example - Ubuntu) and install it through Virtual box.
Get the latest kernel (or of your choice) from kernel.org.
Choose the platform you want to build this kernel for. E.g arm64 or x86.
In case you do not have real boards (e.g RPi for arm variant), you can use qemu-arm64 or qemu-x86 to run your compiled kernel. This is also a good option when users do not have the boards.
Another good use case for using qemu for the newbie kernel developers is even they write some modules which crashes, then the qemu instance is crashed so no harm.
I think using qemu is a good option for people who starts to learn kernel programming and also want to try writing some of their modules and do not intend to purchase hardware at this point of time.
It depends on your target. For your case, you have made a kernel driver for your computer (it run Linux kernel).
But if you want to develop a Kernel driver for another target like Rasberry Pi, ARM board, X86-X64 board, ... you must learn to compile, edit Kernel config, boot Kernel image, ... because each target has different kernel versions.
You can refer to this training for more detail: https://bootlin.com/training/embedded-linux/

GDB Debugging a Raspberry Pi via QEMU

I have multiple questions regarding debugging a Raspberry pi 3 from a linux x64 host using gdb-multiarch, as well as writing bare metal programs in general. We are currently facing a problem where our code appears to not be loaded into memory. When we begin debugging in GDB we start at address 0. 3 instructions down we jump into 0x10000. If I modify my linker script to put the Raspberry pi into either address I get the same result, we jump into 0x10000 and our code isn't loaded there. Instead we get this
We noticed also that GDB is using 32 bit register names here when we're supposed to be debugging 64 bit code.
Again a recap of what we're using:
QEMU with versatile-pb machine.
An aarch64 GCC cross-compiler.
GDB-multiarch.
We've tried on two different hosts: Ubuntu 16.04 x64 Host running in virtualbox. Mint x64 running natively.
We also tried the arm-none-eabi toolchain but were running into problems not having our code compiled as 64 bit.
Help is much appreciated! Thanks!
You don't give your command line, but "versatile-pb" is a 32-bit only board type, so trying to run 64-bit code on it is going to misbehave in confusing ways. You need to tell QEMU to emulate a 64-bit capable board that matches what your bare-metal code is expecting to run on.
In QEMU 2.12 there will be a "raspi3" QEMU board which may be helpful for you; you'd need to try building the latest 2.12 release candidate tarball at the moment if you wanted to experiment with that (2.12 release isn't due for another couple of weeks). Otherwise you could use the "virt" board if you made sure your bare metal code was built to be able to run on that board.

Simple bootloader for running Linux kernel on a simulator

We have built a simple instruction set simulator for the sparc v8 processor. The model consists of a v8 processor, a main memory and a character input and a character output device. Currently I am able to run simple user-level programs on this simulator which are built using a cross compiler and placed in the modeled main memory directly.
I am trying to get a linux kernel to run on this simulator by building a simplest bootloader. (I'm considering uClinux which is made for mmu-less systems). The uncompressed kernel and the filesystem are both assumed to be present in the main memory itself, and all that my bootloader has to do is pass the relevant information to the kernel and make a jump to the start of the kernel code. I have no experience in OS development or porting linux.
I have the following questions :
What is this bare minimum information that a bootloader has to supply to the kernel ?
How to pass this information?
How to point the kernel to use my custom input/output devices?
There is some documentation available for porting linux to ARM boards, and from this documentation, it seems that the bootloader passes information about the size of RAM etc
via a data structure called ATAGS. How is it done in the case of a Sparc processor? I could not find much documentation for Sparc on the internet. There exists a linux bootloader for the Leon3 implementation of Sparc v8, but I could not find the specific information I was looking for in its code.
I will be grateful for any links that explain the bare minimum information to be passed to a kernel and how to pass it.
Thanks,
-neha

Qemu emulator arm for tegra2

I have Toradex Colibri t20 embedded board Linux image (U boot,kernel,roofs). Colibri t20 is based on tegra2 arm-cortex-a9 processor. I want to run this Linux kernel in Qemu emulator.How can i boot with qemu?
qemu-system-arm can run u-boot and kernel SW, only when it has support for the board that those SW were configured/built for.
ARM's versatilepb and vexpress-a9 are supported. There are good tutorials out there for using those boards, Balau on kernel, also Kumar
Bare metal simulation is less ambitious. There, roughly speaking, QEMU only has to emulate the processor. Check out Balau on bare metal. Quick search shows maybe there is support in progress for Tegra2.
I'd suggest installing current packages on your host, see what machines and cpus are supported. Not sure of the following syntax detail, but something like
qemu-system-arm -M ?
is the way to get the list of supported machines.

Playing/Learning -- QEMU (for ARM), Angstrom Linux (or Debian)

My ultimate goal is to do some programming for the Angstrom Linux (or Debian or other Linux distros), on QEMU emulating ARM processor board s.a. Versatile board. I am happy to experiement, but if someone has attempted something similar, and can give little guidance, it might hasten progress.
My understanding of the steps needed are:-
1. Build QEMU from source (although I am not sure if a prebuilt binary won't do). I found QEMuManager on Windows (XP being my Desktop OS on which I intend to run QEMU).
2. Install ARM tool chain (e.g. Yagarto / GNU-ARM for Cygwin?)
3. Download an Angstrom Linux tarball and build it
4. Create a QEMU image with Angstrom Linux.
However I am missing on the details, as I believe there are choices to be made at each of those steps.
IMHO you should use a linux distribution as host machine for your QEmu instead of trying to compile/install all the QEmu stuff in a cygwin based system, it will remove some futur headaches. You can use a VMWare player with an ubuntu image.
I used to play with this tutorial for Debian on QEMU.
The beagleboard, hawkboard, open-rd sites all tend to lead to their distros being built on qemu (arm), and from there there is no reason why you cannot just continue to keep running on the simulation instead of heading for hardware.
This is an example of how to do it with ubuntu.
https://wiki.edubuntu.org/ARM/RootfsFromScratch
Yes it is also possible to cross compile everything as well, I would start with wiki pages that hand hold you through all of the steps. Or as with the hawkboard or beagleboard get a pre-built binary (kernel and root file system) and just boot it and run on that environment and not mess with building everything.

Resources