zenocon
Posts: 6
Joined: Mon Sep 17, 2012 3:25 pm

C code to extract BCM serial number

Mon Oct 01, 2012 2:08 pm

The output of /proc/cpuinfo

Code: Select all

Processor	: ARMv6-compatible processor rev 7 (v6l)
BogoMIPS	: 697.95
Features	: swp half thumb fastmult vfp edsp java tls 
CPU implementer	: 0x41
CPU architecture: 7
CPU variant	: 0x0
CPU part	: 0xb76
CPU revision	: 7

Hardware	: BCM2708
Revision	: 0002
Serial		: 000000001a5ce8e3
How can I read the serial number reported here direct from the hardware?. I looked through the BCM2835 datasheet, and I can't find a register that reports this. I don't know the linux source tree all that well -- I know the answer is in there somewhere, but I was hoping someone might be able to tell me quickly.

I'm writing in C - and I'd prefer to not call out to an external script to parse the string output from /proc/cpuinfo

Thanks in advance

Narishma
Posts: 151
Joined: Wed Nov 23, 2011 1:29 pm

Re: C code to extract BCM serial number

Mon Oct 01, 2012 5:09 pm

Is there a reason you can't just parse it in your C code instead of calling an external script?

Something like:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{
	FILE *f = fopen("/proc/cpuinfo", "r");
	if (!f) {
		return 1;
	}
	
	char line[256]; 
   int serial;
	while (fgets(line, 256, f)) {
		if (strncmp(line, "Serial", 6) == 0) {
			char serial_string[16 + 1];
			serial = atoi(strcpy(serial_string, strchr(line, ':') + 2));
		}
	}

	return fclose(f);
}

zenocon
Posts: 6
Joined: Mon Sep 17, 2012 3:25 pm

Re: C code to extract BCM serial number

Mon Oct 01, 2012 5:11 pm

Sure, I could -- I just wondered what register this serial number is stored in. It would be simpler / more ideal to just read that to get the number.

Narishma
Posts: 151
Joined: Wed Nov 23, 2011 1:29 pm

Re: C code to extract BCM serial number

Mon Oct 01, 2012 6:03 pm

zenocon wrote:Sure, I could -- I just wondered what register this serial number is stored in. It would be simpler / more ideal to just read that to get the number.
I don't think you can do that (read a hardware register) on Linux, unless you write a kernel module that reads the register and exposes it to userspace programs, perhaps through a file in /proc. So you would just end up re-implementing /proc/cpuinfo.

User avatar
DexOS
Posts: 876
Joined: Wed May 16, 2012 6:32 pm
Contact: Website

Re: C code to extract BCM serial number

Mon Oct 01, 2012 7:15 pm

https://github.com/raspberrypi/firmware ... -interface

Its easy under bare metal, but under linux you will most likely need to map it. Take a look pi wiring https://projects.drogon.net/raspberry-pi/wiringpi/
as a example of low level access under the pi and linux.
Batteries not included, Some assembly required.

zenocon
Posts: 6
Joined: Mon Sep 17, 2012 3:25 pm

Re: C code to extract BCM serial number

Mon Oct 01, 2012 7:19 pm

Awesome -- thank you! This is very helpful
DexOS wrote:https://github.com/raspberrypi/firmware ... -interface

Its easy under bare metal, but under linux you will most likely need to map it. Take a look pi wiring https://projects.drogon.net/raspberry-pi/wiringpi/
as a example of low level access under the pi and linux.

Return to “General programming discussion”