From 78ae2839c13611ede6e81f4c4487e1eece2db0f8 Mon Sep 17 00:00:00 2001 From: Simo Piiroinen Date: Tue, 25 Apr 2017 15:43:07 +0300 Subject: [PATCH] [android] Get usb serial number from kernel command line. Fixes JB#38422 While android initialization scripts typically set up the usb serial number, it might happen at wrong time from usb-moded point of view and we in general want to minimize the amount of android init that is needed/used for running sfos. Use usb serial number parsed from kernel command line when initializing default usb properties. Signed-off-by: Simo Piiroinen --- src/usb_moded-android.c | 56 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/src/usb_moded-android.c b/src/usb_moded-android.c index d0dafb9..1f50ebf 100644 --- a/src/usb_moded-android.c +++ b/src/usb_moded-android.c @@ -43,11 +43,65 @@ int android_settings(void) return ret; } +/** Read android serial number from kernel command line + */ +static gchar *get_android_serial(void) +{ + static const char path[] = "/proc/cmdline"; + static const char find[] = "androidboot.serialno="; + static const char pbrk[] = " \t\r\n,"; + + char *res = 0; + FILE *file = 0; + size_t size = 0; + char *data = 0; + + if( !(file = fopen(path, "r")) ) { + log_warning("%s: %s: %m", path, "can't open"); + goto EXIT; + } + + if( getline(&data, &size, file) < 0 ) { + log_warning("%s: %s: %m", path, "can't read"); + goto EXIT; + } + + char *beg = strstr(data, find); + if( !beg ) { + log_warning("%s: no serial found", path); + goto EXIT; + } + + beg += sizeof find - 1; + size_t len = strcspn(beg, pbrk); + if( len < 1 ) { + log_warning("%s: empty serial found", path); + goto EXIT; + } + + res = g_strndup(beg, len); + +EXIT: + + free(data); + + if( file ) + fclose(file); + + return res; +} + /** initialize the basic android values */ void android_init_values(void) { - char *text; + gchar *text; + + if( (text = get_android_serial()) ) + { + write_to_file("/sys/class/android_usb/android0/iSerial", text); + g_free(text); + } text = get_android_manufacturer(); if(text)