I have a short question to ask before putting time for research.

I have an Arduino Leonardo with the LUFA mass storage bootloader flashed. Now what I need to do is to add more functionalities to the bootloader. Because in the final product, the Leonardo board will be connected to a fitness machine (permanently connected) and the machine has Windows OS. Then the Leonardo should be able to read an RFID card (I need to add an RFID reader to the board, each user has his own RFID card) and then mount a remote server directory to the local USB Host based on the selected user.

I will have to use a wifi shield too. My question is this: is it theoretically possible to add all these functionalities to the LUFA mass storage bootloader? Because it's not possible to use the Arduino IDE for this purpose, since I'm not uploading any sketch on top of the bootloader, rather than editing the existing mass storage bootloader with the required functionalities and re-flash the chip. Am I correct? (This is what I understood after some research. And I'm a noob :D).

Could you please elaborate what would be the first step for solving this issue?

  • 2
    I couldn't understand WHY you can't create a sketch running after the bootloader that runs the code. Anyway just write a program (like a sketch in arduino, or use directly the avr tools) and then upload it. I suggest you to upload it in the usual way, but if you don't want the bootloader anymore just use an external programmer: you will overwrite it.
    – frarugi87
    Jan 3, 2017 at 13:21
  • just an addendum: a bootloader is just a simple program wich executes at boot and loads the main program after some operations (e.g. check if a newer program should be downloaded and flash it). If you want to use as the normal program, it's not a bootloader anymore (since it is not used to load), but simply a program
    – frarugi87
    Jan 3, 2017 at 13:23
  • 1
    Ok, I'm kind of slow these days, so sorry for taking so much to understand ;) Well, arduino compilation is somewhat "simpler" than regular one, so a lot of details are hidden. Now you are moving towards a more "standard" way of coding. For the SD card example, go into the arduino source files folder (don't remember where they are) and grab the files SdCard.h and SdCard.cpp. You'll also need Fat16Config.h and SdInfo.h probably, but compilation errors will tell you what are the missing files. Create a copy of MassStorage (will be your program) and inside of it create the folder MyLibs ...
    – frarugi87
    Jan 3, 2017 at 14:28
  • 1
    ... and put the sd card library files inside. Now open the makefile and search the variable SRC. These are all the source files you are compiling, so add the source files you have added (in this case, only SdCard.cpp, since the other are header files). And then you (should) have included the SdCard library to this program. Use it like you would in an arduino sketch (so include the header and then use the class). Just one remark: I think you'll need to rename BootloaderMassStorage.c to BootloaderMassStorage.cpp, since you need classes inside (and then there will be problems with plain C, so ...
    – frarugi87
    Jan 3, 2017 at 14:32
  • 1
    ... maybe you'll need to rename all the files to cpp - and of course change the c extension to cpp also in the makefile). Once done, recompile and flash. There will be a lot of errors, but usually I deal with them one by one, since I'm not much expert and so I usually make a lot of mistakes
    – frarugi87
    Jan 3, 2017 at 14:33

2 Answers 2


@Majenko: I think, that the task IS possible, but it is really hard (and Leonardo may need more memory for that eventually and some shields too and so ...) but if I understand the problem, than the Leonardo should pretends to be mass storage device for that application, and pretend it have some folders and files on it, depending which RFID is used to activate it.

The application probabelly is NOT going to format the MSD because it would lose all the user data on it.

So Leonardo can do following:

  • pretend it is already formatted and have some known filesystem on it (vfat is easy enought and probabelly usable as many USB flash sticks came with it)
  • when the machine detects inserted USB, it ask for type - so it get MSD
  • then it will read partition table - Leonardo can emulate it easily, having "just one partition of given size and type" the partition can even be stored as a file on that fileserver to save Leonardo's memory.
  • next goes reading FAT table and root directory
    • knowing the user from RFID it is possible to select the right directory on file server,
    • then enumerate lengths of all files inside (and directories)
    • make table with the names and "offset"
    • send back the FAT as if those files were ordered on the "disk" so each entry points to the following sector, until the last marks EOF
    • root directory begins after FAT and contains names of the files, their lenghts, dates and starting sectors (and some very basic right like read-only, system and such)
    • then follows files and directories
  • each time the machine asks for a "sector", it is possible to compute, in which file the sector is placed, read the portion of a file (potentially fill it with zeroes to "sector lenght" and return it as requested "sector"

  • if the sector is in directory instead, return the part of directory (that means filenames and such)
  • if the sector is outside it all (should not happen), return just zeroes.

On the other direction:

  • if you get something outside structure, put it to temp. file along with address of such stray "sector"
  • if you get something in a file, just replace the file content on given offset
  • if you get something in directory, you need to compare it with previous version and "rename file", "create file" or "delete file" acordingly.

  • it would probabely need two directories on the server for each user - one for actual files, other for "lost sectors" and "deleted files" which could be "reconected", "undeleted" or "attached later" to the files/directories. The second directory can be emptied after "removing USB MSD" when user with given RFID logouts.

It is lot of work, but basically it could be done. It also needs a lot of accounting on the Leonardo and lot of communication with the fileserver, but it is possible to do.

It suppose, that the files on fileserver would not be modified by somebody else while "mounted" and that the Windows machine would not "format" the USB to some kind of filesystem, the Leonardo does not understand (how to translate to the real files on server).

But for the supposed use I think those two conditions could be reasonable. The MSD probabelly just holds the "user profile" so the machine can put in records of hir/her activiti, some stats and so on, probabely it would be just a few growing files maybe even just in root directory.

Anyway I would not like to write such system, as it is terrifying lot of work and a lot of things could go wrong, if there is a bug (or bad connection).

Also the size of required memory for Leonardo is large - probabelly larger, than the Leonardo actually have, but it could be offloaded to the filesystem too (in "hidden directory" not shown to the machine) - which would be slow and hard to manage - or expanded by HW RAM, which would be a lot faster, but needs nontrivial settings.

(Remebers me, how I wrote resident program for DOS, which had just 130 bytes, which swaped in two 16KB widows of EMS where the rest of program (actually interpret of some pseudocode) was placed (it was like 4 or 6 such 16KB pages, but was able to swap itself acordingly) while the remaining two EMS pages was reserverd for data (like 300-400KB total of pseudocode and data) which was also swapped as needed to access everything. From DOS point of view it was just the 130B resident API, from the view of program, which was using it, it was 400KB engine running FORTH-like program and containing database. The result was like 3x faster processing of 2.000+ employes wages.)

Here are the memory restrictions even more tight, but I thing, that it is possible to do such thing, only it is extremely hard to make it work reliably.

  • Thanks for your detailed answer. I got the general idea about what you explained. Regarding the files that will be written to Leonardo: There will be around 23 small files(no folders) with format "ini", "apt" and "dat". And the Leonardo mass storage has FAT12 file system. And I will extend the storage with a SD card. Yet I have less knowledge in file systems. So I will be spending the next few days on researching about the topics you mentioned. Life is hard xD
    – Isuru
    Jan 4, 2017 at 8:40

From your brief description of what you want to do I think you want to:

  1. Mount a Windows share on your Leonardo
  2. Make said share available through USB MSD

Well, sorry to break it to you, but that is not possible. No, it's not because the Leonardo isn't up to the task, but you are trying to mix two completely different concepts.

A Windows share (CIFS) is a file-centric system. That is, you access files within the share and request bytes at offsets within the files. The client has no concept of the underlying filesystem, it just works on files.

A USB MSD device, in contrast, has no concept of files, directories, etc. Instead all it knows about is blocks. Raw fixed size blocks of data. The computer that it is attached to is what knows what a filesystem is. It requests blocks of data from the MSD and interprets them as a filesystem, allowing you to access the files within it.

You can see the problem you would now face? MSD responding to requests for raw blocks of data from a computer which then wants to get the contents of files from a server - how do they fit together? Simply: they don't.

So you are going to have to re-think your basic idea.

One possible idea would be to use some client-server block based system, such as iSCSI, which gives you block devices on a network (a single file which is an image of a virtual disk), though only the device which is attached to that block device through the network can see the content of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.