aboutsummaryrefslogtreecommitdiffstats
path: root/tools/nv_data-imei.c
blob: aa62e0c13eb425d1e7f74ffe960e74ae681a35e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
/*
 * This file is part of libsamsung-ipc.
 *
 * Copyright (C) 2014 Paul Kocialkowsk <contact@paulk.fr>
 * Copyright (C) 2020 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
 *
 * libsamsung-ipc is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * libsamsung-ipc is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with libsamsung-ipc.  If not, see <http://www.gnu.org/licenses/>.
 */

#define _GNU_SOURCE
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>

#include <samsung-ipc.h>

#include "../samsung-ipc/modems/xmm616/xmm616.h"

#include "nv_data-imei.h"

#define DEBUG 0

#if DEBUG
static int print_offset(struct offset *offset)
{
	printf("offset @ %p: {\n", offset);
	printf("\tsize_t offset: 0x%x\n", offset->offset);
	printf("\tbool option_set: %s\n",
	       offset->option_set ? "True" : "False");
	printf("\tint error: %d\n", offset->error);
	printf("\tchar *optarg: %s\n", offset->optarg);
	printf("}\n");

	return 0;
}

static int print_imei(struct imei *imei)
{
	printf("imei @ %p: {\n", imei);
	printf("\tchar imei[%d + 1]: %s\n", IMEI_LENGTH, imei->imei);
	printf("\tbool option_set: %s\n", imei->option_set ? "True" : "False");
	printf("\tchar *optarg: %s\n", imei->optarg);
	printf("}\n");

	return 0;
}
#endif /* DEBUG */

static int list_supported(void)
{
	/* TODO:
	 * - Print the result in a parsable format (json?)
	 * - Print the result in and a human format (a table for instance)
	 * - Add IMEI location (under the battery, unknown, etc)
	 * - Add IMEI known offsets
	 */
	printf("Supported devices:\n");

	/* Offset: 0xE80, other?
	 * Location: Under the battery
	 */
	printf("\tNexus S (GT-I902x)\n");

	return 0;
}

static int get_offset(const struct command *command, void *arg)
{
	struct offset *offset = arg;
	size_t i;
	int rc;
	bool cmd_has_offset = !!(command->options & OPTION_OFFSET);
	bool cmd_requires_offset = !!(command->required_options &
				      OPTION_OFFSET);

	if (!cmd_has_offset && !offset->option_set)
		return 0;

	if (!cmd_has_offset && offset->option_set) {
		printf("The %s command doesn't have an -o or --offset option\n",
		       command->name);
		printf("See 'nv_data-imei %s -h' for more details\n",
		       command->name);
		return -EINVAL;
	}

	if (cmd_requires_offset  && !offset->option_set) {
		printf("OFFSET option required\n");
		printf("See nv_data-imei %s -h for more details.\n",
		       command->name);
		return -EINVAL;
	} else if (offset->option_set) {
		for (i = 0; i < strlen(offset->optarg); i++) {
			if (isspace(offset->optarg[i])) {
				continue;
			} else {
				if (offset->optarg[i] == '-')
					offset->error |= OFFSET_NEGATIVE;
				break;
			}
		}

		offset->offset = strtoul(offset->optarg, NULL, 0);
		rc = errno;

		if (offset->offset == ULONG_MAX && rc == ERANGE)
			offset->error |= OFFSET_OVERFLOW;

		if ((offset->error & OFFSET_NEGATIVE) &&
		    (offset->error & OFFSET_OVERFLOW))
			printf("Error: The '%s' offset is negative "
			       "and too big as well.\n",
			       offset->optarg);
		else if (offset->error & OFFSET_NEGATIVE)
			printf("Error: The '%s' offset is negative"
			       " but offsets cannot be negative.\n",
			       offset->optarg);
		else if (offset->error & OFFSET_OVERFLOW)
			printf("Error: The '%s' offset is too big.\n",
			       offset->optarg);

		if (offset->error)
			return -EINVAL;
	}

	return 0;
}

static int get_imei(const struct command *command, void *arg)
{
	struct imei *imei = arg;

	if (command->options & OPTION_IMEI) {
		if ((command->required_options & OPTION_IMEI) &&
		    !(imei->option_set)) {
			printf("IMEI option required\n");
			printf("See nv_data-imei %s -h for more details.\n",
			       command->name);
			return -EINVAL;
		} else if (imei->option_set) {
			bool str_is_digit = true;
			bool str_len_valid;
			size_t len;
			size_t i;

			len = strlen(imei->optarg);
			str_len_valid = !!(len == IMEI_LENGTH);

			for (i = 0; i < len; i++) {
				if (!isdigit(imei->optarg[i])) {
					str_is_digit = false;
					break;
				}
			}

			if (!str_is_digit && !str_len_valid) {
				printf("The '%s' "
				       "IMEI is invalid"
				       " as it does not only contains digits\n",
				       imei->optarg);
				printf("In addition it is also invalid"
				       " as it is composed of "
				       "%zd digits instead of %d.\n",
				       len, IMEI_LENGTH);
				return -EINVAL;
			} else if (!str_is_digit) {
				printf("The '%s' "
				       "IMEI is invalid"
				       " as it does not only contains digits\n",
				       imei->optarg);
				return -EINVAL;
			} else if (!str_len_valid) {
				printf("The '%s' "
				       "IMEI is invalid as it is composed of "
				       "%zd digits instead of %d.\n",
				       imei->optarg, len, IMEI_LENGTH);
				return -EINVAL;
			}

			/* imei.imei is IMEI_LENGTH + 1 */
			strncpy(imei->imei, imei->optarg, IMEI_LENGTH);

			return 0;
		}
	} else if (imei->option_set) {
		printf("The %s command doesn't have an -i or --imei option\n",
		       command->name);
		printf("See 'nv_data-imei %s -h' for more details\n",
		       command->name);
		return -EINVAL;
	}

	return 0;
}

static const struct command_option commands_options[] = {
	{
		OPTION_FILE,
		"",
		"",
		"",
		NULL
	},
	{
		OPTION_HELP,
		"-h|--help",
		"Display the command specific help message",
		"-h",
		NULL,
	},
	{
		OPTION_OFFSET,
		"-o OFFSET|--offset=OFFSET",
		"Use the given OFFSET",
		"--offset=0xEC80",
		get_offset,
	},
	{
		OPTION_IMEI,
		"-i IMEI|--imei=IMEI",
		"Use the given IMEI",
		"--imei=355921041234567",
		get_imei,
	},
	{ 0 },
};

static const struct command commands[] = {
	{
		"list-supported",
		"Display supported devices and EFS",
		NO_OPTIONS,
		NO_OPTIONS,
		list_supported,
	},
	{
		"read-imei",
		"Show the current IMEI from nv_data",
		OPTION_FILE|OPTION_OFFSET,
		OPTION_FILE,
		read_imei,
	},
	{
		"write-imei",
		"Store the given IMEI to nv_data (may or may not work)",
		OPTION_FILE|OPTION_IMEI|OPTION_OFFSET,
		OPTION_FILE|OPTION_IMEI|OPTION_OFFSET,
		write_imei,
	},
	{
		"bruteforce-imei",
		"Find the IMEI offset in the nv_data with the given IMEI",
		OPTION_FILE|OPTION_IMEI,
		OPTION_FILE|OPTION_IMEI,
		bruteforce_imei_offset,
	},
	{ 0 },
};

#if DEBUG
static int print_args(int argc, char *argv[], int optind_index)
{
	int i;

	printf("argc: %d optind: %d ", argc, optind_index);
	for (i = 0; i < argc; i++) {
		printf("[%d]%s", i, argv[i]);
		if (i != (argc - 1))
			printf(" ");
	}
	printf("\n");

	return 0;
}
#endif /* DEBUG */

static const char warning_msg[] =
	"\n"
	"+------------------------------------------------------+\n"
	"| /!\\ This tool is experimental, use at your own risk  |\n"
	"+------------------------------------------------------+\n"
	"\n"
	"It is also dangerous if used improperly: This tool can overwrite any\n"
	"part of the nv_data.bin file, without any checks or warnings: If you\n"
	"give it any location/offset inside that file, it will proceed\n"
	"blindly. This raises several concerns:\n"
	"- As we don't know how to recreate valid nv_data.bin files from\n"
	"  scratch, so you will need to make a backup of it before. We can\n"
	"  recreate a dummy one, however the result is that it ends up with a\n"
	"  generic IMEI. This means that most networks will refuse to let you\n"
	"  register and use their services, which will prevent you from doing\n"
	"  regular calls / SMS. Changing the IMEI in this generic file has\n"
	"  not been tested. It's unknown if some other parameters also need\n"
	"  to be changed to make it work.\n"
	"- As we don't know much about the content of the nv_data.bin file,\n"
	"  it could potentially be dangerous to modify data in the wrong\n"
	"  location as we don't know the effects. Effects like disrupting the\n"
	"  telephony network which could in turn prevent people from calling\n"
	"  medial emergency services cannot be excluded as this file contains\n"
	"  modem data / parameters and we don't know what they do.\n"
	"\n"
	"How to use this program.\n"
	"- First you need to obtain your current IMEI. This can be done\n"
	"  through various ways, from looking at the sticker that is written\n"
	"  under the back cover of your phone, to software means like looking\n"
	"  in Settings->About Phone->Status->IMEI information in Replicant 6.\n"
	"- Then once you have the IMEI you can either use the show-imei\n"
	"  command and verify that the text you get matches the IMEI you're\n"
	"  supposed to have, or you can also try to bruteforce the IMEI\n"
	"  location with the known IMEI.\n"
	"- Once this is done you can change the IMEI, and confirm it has been\n"
	"  changed by looking at\n"
	"  Settings->About Phone->Status->IMEI information in Replicant 6 or\n"
	"  through other software means.\n"
	"\n"
	"In the case where you don't have a valid IMEI, and you are still\n"
	"trying to change it (knowing the risks), you will need not to forget\n"
	"to verify if it has been changed by looking at\n"
	"Settings->About Phone->Status->IMEI information in Replicant 6 or\n"
	"through other software means. This will make sure that you only\n"
	"modified the offset where the IMEI is really stored and not some\n"
	"random offset with potentially crucial modem data.\n"
;

static void print_warnings(void)
{
	printf("%s", warning_msg);
}

/* TODO: Enforce type to only allow valid OPTION_* */
static const struct command_option *get_option(uint8_t given_option)
{
	int i = 0;

	while (true) {
		const struct command_option *command_option =
			&(commands_options[i++]);

		/* TODO: Get C to do something like if (!option) */
		if (!command_option->option)
			break;

		if (command_option->option == given_option)
			return command_option;
	}

	return NULL;
}

static int print_all_options(void)
{
	int i = 0;

	while (true) {
		const struct command_option *option = &(commands_options[i++]);

		/* TODO: Get C to do something like if (!option) */
		if (!option->option)
			break;

		/* Skip options without help like OPTION_FILE */
		if (strlen(option->option_string))
			printf("\t%s # %s\n", option->option_string,
			       option->help);
	}

	return 0;
}

static int nv_data_imei_help(void)
{
	int i = 0;

	print_warnings();
	printf("\n");

	printf("Usage:\n");
	printf("\tnv_data-imei FILE COMMAND [OPTIONS]\n");
	printf("\tnv_data-imei COMMAND -h|--help "
		"# Display the command specific help message\n");

	printf("Commands:\n");

	while (true) {
		const struct command *cmd = &(commands[i++]);

		/* TODO: Get C to do something like if (!cmd) */
		if (!cmd->name)
			break;

		assert(cmd->name);
		assert(cmd->help);
		printf("\t%s # %s\n", cmd->name, cmd->help);
	}

	printf("Options:\n");
	print_all_options();

	return 0;
}

static const struct command *get_command(const char *name)
{
	int i = 0;

	while (true) {
		const struct command *cmd = &(commands[i++]);

		/* TODO: Get C to do something like if (!cmd) */
		if (!cmd->name)
			break;

		if (strlen(cmd->name) != strlen(name))
			continue;

		if (!strncmp(cmd->name, name, strlen(cmd->name)))
			return cmd;
	}

	return NULL;

}

static int command_help(const char *command_name)
{

	const struct command *command;
	size_t i;

	command = get_command(command_name);
	if (!command)
		return EX_USAGE;

	printf("Usage:\n");

	printf("\tnv_data-imei %s%s",
	       (command->options & OPTION_FILE) ? "FILE " : "",
	       command->name);

	if (command->options) {
		for (i = 0; i < (8 * sizeof(command->options)); i++) {
			if (command->options & BIT(i)) {
				bool required = !!(command->required_options &
						   BIT(i));
				const struct command_option *option =
					get_option(command->options & BIT(i));

				/* Check if option and commands are in sync */
				assert(option != NULL);

				if (strlen(option->option_string)) {
					if (required)
						printf(" <%s>",
						       option->option_string);
					else
						printf(" [%s]",
						       option->option_string);
				}
			}
		}
	}

	printf("\n");

	if (command->options) {
		printf("Options:\n");
		for (i = 0; i < (8 * sizeof(command->options)); i++) {
			if (command->options & BIT(i)) {
				const struct command_option *option =
					get_option(command->options & BIT(i));

				/* Check if option and commands are in sync */
				assert(option != NULL);

				if (strlen(option->option_string))
					printf("\t%s #%s\n",
					       option->option_string,
					       option->help);
			}
		}
	}

	printf("Example:\n");

	printf("\tnv_data-imei %s%s",
	       (command->options & OPTION_FILE) ?
	       "./efs_backup_copy/nv_data.bin" : "",
	       command->name);

	if (command->options) {
		for (i = 0; i < (8 * sizeof(command->options)); i++) {
			if (command->required_options & BIT(i)) {
				const struct command_option *option =
					get_option(command->options & BIT(i));

				/* Check if option and commands are in sync */
				assert(option != NULL);

				printf(" %s", option->example);
			}
		}
	}

	printf("\n");

	return 0;
}

static void modem_log_handler(__attribute__((unused)) void *user_data,
			      const char *msg)
{
	int i, l;

	char *message;

	message = strdup(msg);
	l = strlen(message);

	if (l > 1) {
		for (i = l ; i > 0 ; i--) {
			if (message[i] == '\n')
				message[i] = 0;
			else if (message[i] != 0)
				break;
		}
		printf("%s\n", message);
	}

	free(message);
}

static int ipc_setup(struct ipc_client **client)
{
	*client = ipc_client_create(IPC_CLIENT_TYPE_DUMMY);
	if (*client == NULL) {
		printf("Creating client failed\n");
		return -EBADE;
	}

	ipc_client_log_callback_register(*client, modem_log_handler,
					 NULL);

	return 0;
}

static int decode_imei(unsigned char *buf, struct imei *imei)
{
	int i = 0;

	i += snprintf(imei->imei + i, IMEI_LENGTH + 1 - i, "%01x",
		      (*buf & 0xf0) >> 4);

	buf += sizeof(unsigned char);

	while (i < IMEI_LENGTH) {
		i += snprintf(imei->imei + i, IMEI_LENGTH + 1 - i,
			      "%02x",
			      (*buf >> 4) | ((*buf & 0x0f) << 4));
		buf += sizeof(unsigned char);
	}

	return 0;
}

static int encode_imei(unsigned char *buf, struct imei *imei)
{
	int i = 0;
	unsigned int v;
	int count = 0;

	count = sscanf(imei->imei, "%01x", &v);
	if (count != 1) {
		printf("%s: first sscanf failed with result: %d\n",
		       __func__, count);
		assert(false);
	}

	*buf++ = (v << 4) | 0xA;

	i++;
	while (i < IMEI_LENGTH) {
		count = sscanf(imei->imei + i, "%02x", &v);
		if (count != 1) {
			printf("%s: second sscanf failed with result: %d\n",
			       __func__, count);
			assert(false);
		}

		*buf++ = v << 4 | ((v & 0xf0) >> 4);
		i += 2;
	}

	return 0;
}

int bruteforce_imei_offset(char *nv_data_path, struct imei *given_imei)
{
	struct ipc_client *client = NULL;
	size_t file_size;
	size_t search_size;
	size_t nv_data_chunk_size;
	char *buffer = NULL;
	char *ptr = NULL;
	unsigned char given_imei_buffer[(IMEI_LENGTH + 1) / 2] = { 0 };
	bool found_imei = false;
	int rc;

	rc = ipc_setup(&client);
	if (rc)
		return rc;

	ipc_client_log(client,
		       "Starting bruteforce\nnv_data_path: %s",
		       nv_data_path);

	/* The sizes are device dependent, so ipc_client_nv_data_size and
	 * ipc_client_nv_data_chunk_size were used before.
	 * However we want the tool to also be able to run on any computer,
	 * instead of just being able to run on a device.
	 */
	file_size = file_data_size(client, nv_data_path);
	if (file_size == (size_t)-1) {
		rc = errno;
		goto error;
	}

	ipc_client_log(client, "nv_data size: %d\n", file_size);

	/* We only support one device so far */
	nv_data_chunk_size = XMM616_NV_DATA_CHUNK_SIZE;

	buffer = file_data_read(client, nv_data_path, file_size,
				nv_data_chunk_size, 0);

	if (buffer == NULL) {
		ipc_client_log(client, "Reading nv_data failed");
		rc = -1;
		goto error;
	}

	rc = encode_imei((unsigned char *)&given_imei_buffer, given_imei);
	if (rc < 0)
		return rc;

	ptr = buffer;
	search_size = file_size;

	do {
		ptr = memchr(ptr, given_imei_buffer[0], search_size);
		if (ptr) {
			if (!strncmp((void*)given_imei_buffer, ptr,
				     sizeof(given_imei_buffer))) {
				ipc_client_log(client,
					       "=> Found IMEI at 0x%x (%d)",
					       (ptr - buffer),
					       (ptr - buffer));
				found_imei = true;
			}

			/* Continue searching even if we already found
			 * it just in case we find the IMEI at a second
			 * location too.
			 */
			search_size = file_size - (ptr - buffer);
			ptr ++;
		}
	} while (ptr);

	if (!found_imei) {
		rc = 0;
		ipc_client_log(client, "=> IMEI not found");
	}

error:
	if (buffer)
		free(buffer);

	ipc_client_destroy(client);

	return rc;
}

int read_imei(char *nv_data_path, struct offset *offset)
{
	struct ipc_client *client = NULL;
	struct imei imei;
	size_t file_size;
	size_t nv_data_chunk_size;
	unsigned char *buffer = NULL;
	int rc;

	memset(&imei, 0, sizeof(imei));

	rc = ipc_setup(&client);
	if (rc)
		return rc;

	/* We only support one device so far */
	file_size = XMM616_NV_DATA_SIZE;
	nv_data_chunk_size = XMM616_NV_DATA_CHUNK_SIZE;

	buffer = file_data_read(client, nv_data_path, file_size,
				nv_data_chunk_size, 0);
	if (buffer == NULL) {
		ipc_client_log(client, "Reading nv_data failed\n");
		rc = -1;
		goto error;
	}

	rc = decode_imei(buffer + offset->offset, &imei);
	if (rc)
		goto error;

	ipc_client_log(client, "IMEI: %s\n", imei.imei);

	rc = 0;
	goto complete;

error:
complete:
	if (buffer)
		free(buffer);

	ipc_client_destroy(client);

	return rc;
}

int write_imei(char *nv_data_path, struct offset *offset,
		      struct imei *imei)
{
	struct ipc_client *client = NULL;
	char *md5_path = NULL;
	char *nv_data_secret;
	size_t nv_data_chunk_size;
	size_t file_size;
	char *md5_string = NULL;
	unsigned char buffer[(IMEI_LENGTH + 1) / 2] = { 0 };
	size_t length;
	int rc;

	rc = ipc_setup(&client);
	if (rc)
		return rc;

	assert(imei->imei);
	assert(strlen(imei->imei) == IMEI_LENGTH);

	rc = asprintf(&md5_path, "%s.md5", nv_data_path);
	if (rc == -1) {
		ipc_client_log(client, "%s: asprintf failed", __func__);
		return -1;
	}

	/* We only support one device so far */
	nv_data_secret = XMM616_NV_DATA_SECRET;
	file_size = XMM616_NV_DATA_SIZE;
	nv_data_chunk_size = XMM616_NV_DATA_CHUNK_SIZE;

	rc = encode_imei((unsigned char *)&buffer, imei);
	if (rc < 0)
		return rc;

	rc = file_data_write(client, nv_data_path, buffer, sizeof(buffer),
			     sizeof(buffer), offset->offset);
	if (rc == -1) {
		rc = errno;
		ipc_client_log(client, "Writing nv_data failed\n");
		goto complete;
	}

	md5_string = ipc_nv_data_md5_calculate(client, nv_data_path,
					       nv_data_secret, file_size,
					       nv_data_chunk_size);
	if (md5_string == NULL) {
		ipc_client_log(client, "Calculating nv_data md5 failed\n");
		goto error;
	}

	length = strlen(md5_string);

	unlink(md5_path);

	rc = file_data_write(client, md5_path, md5_string, length, length, 0);
	if (rc == -1) {
		rc = errno;
		ipc_client_log(client, "Writing nv_data md5 failed\n");
		goto complete;
	}

	rc = 0;
	goto complete;

error:
	rc = -1;

complete:
	if (md5_path)
		free(md5_path);

	ipc_client_destroy(client);

	return rc;
}

static int errno_to_sysexit(int err)
{
	switch (err) {
	case 0:
		return EX_OK;
	case EACCES:
		return EX_NOINPUT;
	case -EINVAL:
		return EX_USAGE;
	default:
		printf("%s: error: unknown error code %d.\n", __func__, err);
		printf("%s: error code %d needs to be implemented\n", __func__,
		       err);
		assert(false);
	}

	return 0;
}

int main(int argc, char * const argv[])
{
	opterr = 0;
	struct imei imei;
	struct offset offset;
	const struct command *command = NULL;
	const struct command_option *option = NULL;
	char *nv_data_path = NULL;
	int c, rc;

	memset(&imei, 0, sizeof(imei));
	memset(&offset, 0, sizeof(offset));

	if (argc == 1) {
		printf("Not enough options.\n");
		printf("Try -h to print the help.\n");
		return EX_USAGE;
	}

	while (1) {
		static struct option long_options[] = {
			{"help", no_argument, 0, 'h' },
			{"imei", required_argument, 0, 'i' },
			{"offset", required_argument, 0, 'o' },
			{0, 0, 0, 0 }
		};

		c = getopt_long(argc, argv, "-hi:o:", long_options, NULL);
		if (c == -1)
			break;

		switch (c) {
		case 1:
			/* from "man 3 getopt": If the first character of
			 * optstring is '-', then each nonoption argv-element is
			 * handled as if it were the argument of an option with
			 * character code 1. (This is used by programs that were
			 * written to expect options and other argv-elements in
			 * any order and that care about the ordering of the
			 * two.)
			 */

			/* nv_data-imei <argument> */
			if (optind == 2 && argc == 2) {
				command = get_command(argv[optind - 1]);
				/* Example: nv_data-imei list-supported */
				if (command && command->options == NO_OPTIONS) {
					rc = command->func();
					return errno_to_sysexit(rc);
				} else if (command) {
					/* The other commands than
					 * list-supported need at least a file
					 * argument so we don't need to handle
					 * all missing options
					 */
					if (command->options & OPTION_FILE) {
						printf("Error: the '%s' command "
						       "needs a FILE argument.\n",
						       argv[optind - 1]);
						printf("See 'nv_data-imei %s -h'"
						       " for more details.\n",
						       argv[optind - 1]);
					}
					return EX_USAGE;
				} else {
					printf("There is no '%s' command\n",
					       argv[optind - 1]);
					printf("Try nv_data-imei -h"
					       " to print the help.\n");
					return EX_USAGE;
				}
			/* nv_data-imei FILE COMMAND [...] */
			} else if (optind == 3 && argc >= 3) {
				nv_data_path = argv[optind - 2];
				command = get_command(argv[optind - 1]);
				if (!command) {
					printf("There is no '%s' command\n",
					       argv[optind - 1]);
					printf("Try nv_data-imei -h"
					       " to print the help.\n");
					return EX_USAGE;
				}
				/* Some commands don't take arguments nor files.
				 * The help command is already handled but some
				 * other command like list-supported need to be
				 * handled here.
				 * nv_data-imei FILE list-supported
				 */
				if (!command->options) {
					printf("The '%s' command"
					       " accepts no options\n",
					       argv[2]);
					printf("Try nv_data-imei %s --help to"
					       " print the %s command help.\n",
					       argv[optind - 1],
					       argv[optind - 1]);
					return EX_USAGE;
				}
			}
			break;
		case 'h':
			/* nv_data-imei -h|--help */
			if (argc == 2) {
				return nv_data_imei_help();

			/* nv_data-imei COMMAND -h|--help */
			} else if (argc == 3) {
				rc = command_help(argv[1]);
				if (rc) {
					printf("There is no '%s' command\n",
					       argv[1]);
					printf("Try nv_data-imei -h"
					       " to print the help.\n");
					return rc;
				}

				/* nv_data-imei FILE COMMAND -h|--help|help is
				 * not supported because I didn't find an easy
				 * and robust way to differentiate between
				 * argv[1] being a file or a command. In other
				 * words If it was, supported, and that we are
				 * here, what would be the command? argv[2] or
				 * argv[3]? How to know for sure that argv[2] is
				 * really a command and not a file of the same
				 * name than the command?
				 */

				return 0;
			} else if (argc > 3) {
				printf("Wrong number of arguments."
				       " Try 'nv_data-imei COMMAND -h' instead"
				       "\n");
				printf("Example:\n");
				printf("\tnv_data-imei %s -h\n",
				       commands[0].name);
				return EX_USAGE;
			}
			break;
		case 'i':
			imei.optarg = optarg;
			if (imei.option_set) {
				printf("The %s command doesn't have an -i"
				       " or --imei option\n",
				       command->name);
				printf("See 'nv_data-imei %s -h'"
				       " for more details\n",
				       commands->name);
				return EX_USAGE;
			}

			imei.option_set = true;

			break;
		case 'o':
			offset.optarg = optarg;
			if (offset.option_set) {
				printf("The %s command"
				       " doesn't have an -o or --offset option"
				       "\n", command->name);
				printf("See 'nv_data-imei %s -h'"
				       " for more details\n",
				       commands->name);
				return EX_USAGE;
			}

			offset.option_set = true;

			break;
		case '?':
			printf("Unknown option '%s'.\n", argv[optind - 1]);
			printf("Try nv_data-imei -h to print the help.\n");
			return EX_USAGE;
		default:
			printf("case '%c':\n", c);
			printf("Unknown option '%s'.\n", argv[optind - 1]);
			printf("Try nv_data-imei -h to print the help.\n");
			return EX_USAGE;
		}
	}

	/* We use the - in optstring so all arguments go in the 'case 1:' */
	assert(optind == argc);

	if (argc == 2 || command == NULL) {
		/* If none of the commands or options were reached, we are in
		 * the case where users ran 'nv_data-imei FILE' or used some
		 * options like -i <argument> without any command.
		 */
		printf("Missing options, command or invalid command '%s'\n",
		       argv[1]);
		printf("Try -h to print the help.\n");
		return EX_USAGE;
	}

	assert(command->options & OPTION_FILE);
	assert(command->func);


	option = get_option(OPTION_IMEI);
	rc = option->get_data(command, &imei);
	if (rc)
		return errno_to_sysexit(rc);

	option = get_option(OPTION_OFFSET);

	rc = option->get_data(command, &offset);
	if (rc)
		return errno_to_sysexit(rc);

	if (command->options & OPTION_IMEI &&
	    command->options & OPTION_OFFSET) {
		rc = command->func(nv_data_path, &offset, &imei);
		return errno_to_sysexit(rc);
	}

	if (command->options & OPTION_IMEI) {
		rc = command->func(nv_data_path, &imei);
		return errno_to_sysexit(rc);
	}

	if (command->options & OPTION_OFFSET) {
		rc = command->func(nv_data_path, &offset);
		return errno_to_sysexit(rc);
	}

	assert(false);

	return 0;
}