Boot Linux faster!

Check our new training course

Boot Linux faster!

Check our new training course
and Creative Commons CC-BY-SA
lecture and lab materials

Bootlin logo

Elixir Cross Referencer

   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
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
/*
 *
 *  oFono - Open Source Telephony
 *
 *  Copyright (C) 2008-2011  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define _GNU_SOURCE
#include <string.h>
#include <stdio.h>

#include <glib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <dirent.h>

#include "ofono.h"

#include "simfs.h"
#include "simutil.h"
#include "storage.h"

#define SIM_CACHE_MODE 0600
#define SIM_CACHE_BASEPATH STORAGEDIR "/%s-%i"
#define SIM_CACHE_VERSION SIM_CACHE_BASEPATH "/version"
#define SIM_CACHE_PATH SIM_CACHE_BASEPATH "/%04x"
#define SIM_CACHE_HEADER_SIZE 39
#define SIM_FILE_INFO_SIZE 7
#define SIM_IMAGE_CACHE_BASEPATH STORAGEDIR "/%s-%i/images"
#define SIM_IMAGE_CACHE_PATH SIM_IMAGE_CACHE_BASEPATH "/%d.xpm"

#define SIM_FS_VERSION 2

static gboolean sim_fs_op_next(gpointer user_data);
static gboolean sim_fs_op_read_record(gpointer user);
static gboolean sim_fs_op_read_block(gpointer user_data);

struct sim_fs_op {
	int id;
	unsigned char *buffer;
	enum ofono_sim_file_structure structure;
	unsigned short offset;
	gboolean info_only;
	int num_bytes;
	int length;
	int record_length;
	int current;
	unsigned char path[6];
	unsigned char path_len;
	gconstpointer cb;
	gboolean is_read;
	void *userdata;
	struct ofono_sim_context *context;
};

static void sim_fs_op_free(struct sim_fs_op *node)
{
	g_free(node->buffer);
	g_free(node);
}

struct sim_fs {
	GQueue *op_q;
	gint op_source;
	unsigned char bitmap[32];
	int fd;
	struct ofono_sim *sim;
	const struct ofono_sim_driver *driver;
	GSList *contexts;
};

void sim_fs_free(struct sim_fs *fs)
{
	if (fs == NULL)
		return;

	if (fs->op_source) {
		g_source_remove(fs->op_source);
		fs->op_source = 0;
	}

	/*
	 * Note: users of sim_fs must not assume that the callback happens
	 * for operations still in progress
	 */
	if (fs->op_q) {
		g_queue_foreach(fs->op_q, (GFunc) sim_fs_op_free, NULL);
		g_queue_free(fs->op_q);
		fs->op_q = NULL;
	}

	while (fs->contexts)
		sim_fs_context_free(fs->contexts->data);

	g_free(fs);
}

struct file_watch {
	struct ofono_watchlist_item item;
	int ef;
};

struct ofono_sim_context {
	struct sim_fs *fs;
	struct ofono_watchlist *file_watches;
};

struct sim_fs *sim_fs_new(struct ofono_sim *sim,
				const struct ofono_sim_driver *driver)
{
	struct sim_fs *fs;

	fs = g_try_new0(struct sim_fs, 1);
	if (fs == NULL)
		return NULL;

	fs->sim = sim;
	fs->driver = driver;
	fs->fd = -1;

	return fs;
}

struct ofono_sim_context *sim_fs_context_new(struct sim_fs *fs)
{
	struct ofono_sim_context *context =
		g_try_new0(struct ofono_sim_context, 1);

	if (context == NULL)
		return NULL;

	context->fs = fs;
	fs->contexts = g_slist_prepend(fs->contexts, context);

	return context;
}

void sim_fs_context_free(struct ofono_sim_context *context)
{
	struct sim_fs *fs = context->fs;
	int n = 0;
	struct sim_fs_op *op;

	if (fs->op_q) {
		while ((op = g_queue_peek_nth(fs->op_q, n)) != NULL) {
			if (op->context != context) {
				n += 1;
				continue;
			}

			if (n == 0) {
				op->cb = NULL;

				n += 1;
				continue;
			}

			sim_fs_op_free(op);
			g_queue_remove(fs->op_q, op);
		}
	}

	if (context->file_watches)
		__ofono_watchlist_free(context->file_watches);

	fs->contexts = g_slist_remove(fs->contexts, context);
	g_free(context);
}

unsigned int sim_fs_file_watch_add(struct ofono_sim_context *context, int id,
					ofono_sim_file_changed_cb_t cb,
					void *userdata,
					ofono_destroy_func destroy)
{
	struct file_watch *watch;

	if (cb == NULL)
		return 0;

	if (context->file_watches == NULL)
		context->file_watches = __ofono_watchlist_new(g_free);

	watch = g_new0(struct file_watch, 1);

	watch->ef = id;
	watch->item.notify = cb;
	watch->item.notify_data = userdata;
	watch->item.destroy = destroy;

	return __ofono_watchlist_add_item(context->file_watches,
					(struct ofono_watchlist_item *) watch);
}

void sim_fs_file_watch_remove(struct ofono_sim_context *context,
				unsigned int id)
{
	__ofono_watchlist_remove_item(context->file_watches, id);
}

void sim_fs_notify_file_watches(struct sim_fs *fs, int id)
{
	GSList *l;

	for (l = fs->contexts; l; l = l->next) {
		struct ofono_sim_context *context = l->data;
		GSList *k;

		for (k = context->file_watches->items; k; k = k->next) {
			struct file_watch *w = k->data;
			ofono_sim_file_changed_cb_t notify = w->item.notify;

			if (id == -1 || w->ef == id)
				notify(w->ef, w->item.notify_data);
		}
	}

}

static void sim_fs_end_current(struct sim_fs *fs)
{
	struct sim_fs_op *op = g_queue_pop_head(fs->op_q);

	if (g_queue_get_length(fs->op_q) > 0)
		fs->op_source = g_idle_add(sim_fs_op_next, fs);

	if (fs->fd != -1) {
		TFR(close(fs->fd));
		fs->fd = -1;
	}

	memset(fs->bitmap, 0, sizeof(fs->bitmap));

	sim_fs_op_free(op);
}

static void sim_fs_op_error(struct sim_fs *fs)
{
	struct sim_fs_op *op = g_queue_peek_head(fs->op_q);

	if (op->cb == NULL) {
		sim_fs_end_current(fs);
		return;
	}

	if (op->info_only == TRUE)
		((sim_fs_read_info_cb_t) op->cb)
			(0, 0, 0, 0, op->userdata);
	else if (op->is_read == TRUE)
		((ofono_sim_file_read_cb_t) op->cb)
			(0, 0, 0, 0, 0, op->userdata);
	else
		((ofono_sim_file_write_cb_t) op->cb)
			(0, op->userdata);

	sim_fs_end_current(fs);
}

static gboolean cache_block(struct sim_fs *fs, int block, int block_len,
				const unsigned char *data, int num_bytes)
{
	int offset;
	int bit;
	ssize_t r;
	unsigned char b;

	if (fs->fd == -1)
		return FALSE;

	if (lseek(fs->fd, block * block_len +
				SIM_CACHE_HEADER_SIZE, SEEK_SET) == (off_t) -1)
		return FALSE;

	r = TFR(write(fs->fd, data, num_bytes));

	if (r != num_bytes)
		return FALSE;

	/* update present bit for this block */
	offset = block / 8;
	bit = block % 8;

	/* lseek to correct byte (skip file info) */
	lseek(fs->fd, offset + SIM_FILE_INFO_SIZE, SEEK_SET);

	b = fs->bitmap[offset];
	b |= 1 << bit;

	r = TFR(write(fs->fd, &b, sizeof(b)));

	if (r != sizeof(b))
		return FALSE;

	fs->bitmap[offset] = b;

	return TRUE;
}

static void sim_fs_op_write_cb(const struct ofono_error *error, void *data)
{
	struct sim_fs *fs = data;
	struct sim_fs_op *op = g_queue_peek_head(fs->op_q);
	ofono_sim_file_write_cb_t cb = op->cb;

	if (cb == NULL) {
		sim_fs_end_current(fs);
		return;
	}

	if (error->type == OFONO_ERROR_TYPE_NO_ERROR)
		cb(1, op->userdata);
	else
		cb(0, op->userdata);

	sim_fs_end_current(fs);
}

static void sim_fs_op_read_block_cb(const struct ofono_error *error,
					const unsigned char *data, int len,
					void *user)
{
	struct sim_fs *fs = user;
	struct sim_fs_op *op = g_queue_peek_head(fs->op_q);
	int start_block;
	int end_block;
	int bufoff;
	int dataoff;
	int tocopy;

	if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
		sim_fs_op_error(fs);
		return;
	}

	start_block = op->offset / 256;
	end_block = (op->offset + (op->num_bytes - 1)) / 256;

	if (op->current == start_block) {
		bufoff = 0;
		dataoff = op->offset % 256;
		tocopy = MIN(256 - op->offset % 256,
				op->num_bytes - op->current * 256);
	} else {
		bufoff = (op->current - start_block - 1) * 256 +
				op->offset % 256;
		dataoff = 0;
		tocopy = MIN(256, op->num_bytes - op->current * 256);
	}

	DBG("bufoff: %d, dataoff: %d, tocopy: %d",
				bufoff, dataoff, tocopy);

	memcpy(op->buffer + bufoff, data + dataoff, tocopy);
	cache_block(fs, op->current, 256, data, len);

	if (op->cb == NULL) {
		sim_fs_end_current(fs);
		return;
	}

	op->current++;

	if (op->current > end_block) {
		ofono_sim_file_read_cb_t cb = op->cb;

		cb(1, op->num_bytes, 0, op->buffer,
				op->record_length, op->userdata);

		sim_fs_end_current(fs);
	} else {
		fs->op_source = g_idle_add(sim_fs_op_read_block, fs);
	}
}

static gboolean sim_fs_op_read_block(gpointer user_data)
{
	struct sim_fs *fs = user_data;
	struct sim_fs_op *op = g_queue_peek_head(fs->op_q);
	int start_block;
	int end_block;
	unsigned short read_bytes;

	fs->op_source = 0;

	if (op->cb == NULL) {
		sim_fs_end_current(fs);
		return FALSE;
	}

	start_block = op->offset / 256;
	end_block = (op->offset + (op->num_bytes - 1)) / 256;

	if (op->current == start_block) {
		op->buffer = g_try_new0(unsigned char, op->num_bytes);

		if (op->buffer == NULL) {
			sim_fs_op_error(fs);
			return FALSE;
		}
	}

	while (fs->fd != -1 && op->current <= end_block) {
		int offset = op->current / 8;
		int bit = 1 << op->current % 8;
		int bufoff;
		int seekoff;
		int toread;

		if ((fs->bitmap[offset] & bit) == 0)
			break;

		if (op->current == start_block) {
			bufoff = 0;
			seekoff = SIM_CACHE_HEADER_SIZE + op->current * 256 +
				op->offset % 256;
			toread = MIN(256 - op->offset % 256,
					op->num_bytes - op->current * 256);
		} else {
			bufoff = (op->current - start_block - 1) * 256 +
					op->offset % 256;
			seekoff = SIM_CACHE_HEADER_SIZE + op->current * 256;
			toread = MIN(256, op->num_bytes - op->current * 256);
		}

		DBG("bufoff: %d, seekoff: %d, toread: %d",
				bufoff, seekoff, toread);

		if (lseek(fs->fd, seekoff, SEEK_SET) == (off_t) -1)
			break;

		if (TFR(read(fs->fd, op->buffer + bufoff, toread)) != toread)
			break;

		op->current += 1;
	}

	if (op->current > end_block) {
		ofono_sim_file_read_cb_t cb = op->cb;

		cb(1, op->num_bytes, 0, op->buffer,
				op->record_length, op->userdata);

		sim_fs_end_current(fs);

		return FALSE;
	}

	if (fs->driver->read_file_transparent == NULL) {
		sim_fs_op_error(fs);
		return FALSE;
	}

	read_bytes = MIN(op->length - op->current * 256, 256);
	fs->driver->read_file_transparent(fs->sim, op->id,
						op->current * 256,
						read_bytes,
						op->path_len ? op->path : NULL,
						op->path_len,
						sim_fs_op_read_block_cb, fs);

	return FALSE;
}

static void sim_fs_op_retrieve_cb(const struct ofono_error *error,
					const unsigned char *data, int len,
					void *user)
{
	struct sim_fs *fs = user;
	struct sim_fs_op *op = g_queue_peek_head(fs->op_q);
	int total = op->length / op->record_length;
	ofono_sim_file_read_cb_t cb = op->cb;

	if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
		sim_fs_op_error(fs);
		return;
	}

	cache_block(fs, op->current - 1, op->record_length,
			data, op->record_length);

	if (cb == NULL) {
		sim_fs_end_current(fs);
		return;
	}

	cb(1, op->length, op->current, data, op->record_length, op->userdata);

	if (op->current < total) {
		op->current += 1;
		fs->op_source = g_idle_add(sim_fs_op_read_record, fs);
	} else {
		sim_fs_end_current(fs);
	}
}

static gboolean sim_fs_op_read_record(gpointer user)
{
	struct sim_fs *fs = user;
	struct sim_fs_op *op = g_queue_peek_head(fs->op_q);
	const struct ofono_sim_driver *driver = fs->driver;
	int total = op->length / op->record_length;
	unsigned char buf[256];

	fs->op_source = 0;

	if (op->cb == NULL) {
		sim_fs_end_current(fs);
		return FALSE;
	}

	while (fs->fd != -1 && op->current <= total) {
		int offset = (op->current - 1) / 8;
		int bit = 1 << ((op->current - 1) % 8);
		ofono_sim_file_read_cb_t cb = op->cb;

		if ((fs->bitmap[offset] & bit) == 0)
			break;

		if (lseek(fs->fd, (op->current - 1) * op->record_length +
				SIM_CACHE_HEADER_SIZE, SEEK_SET) == (off_t) -1)
			break;

		if (TFR(read(fs->fd, buf, op->record_length)) !=
				op->record_length)
			break;

		cb(1, op->length, op->current,
				buf, op->record_length, op->userdata);

		op->current += 1;
	}

	if (op->current > total) {
		sim_fs_end_current(fs);

		return FALSE;
	}

	switch (op->structure) {
	case OFONO_SIM_FILE_STRUCTURE_FIXED:
		if (driver->read_file_linear == NULL) {
			sim_fs_op_error(fs);
			return FALSE;
		}

		driver->read_file_linear(fs->sim, op->id, op->current,
						op->record_length,
						NULL, 0,
						sim_fs_op_retrieve_cb, fs);
		break;
	case OFONO_SIM_FILE_STRUCTURE_CYCLIC:
		if (driver->read_file_cyclic == NULL) {
			sim_fs_op_error(fs);
			return FALSE;
		}

		driver->read_file_cyclic(fs->sim, op->id, op->current,
						op->record_length,
						NULL, 0,
						sim_fs_op_retrieve_cb, fs);
		break;
	default:
		ofono_error("Unrecognized file structure, this can't happen");
	}

	return FALSE;
}

static void sim_fs_op_cache_fileinfo(struct sim_fs *fs,
					const struct ofono_error *error,
					int length,
					enum ofono_sim_file_structure structure,
					int record_length,
					const unsigned char access[3],
					unsigned char file_status)
{
	struct sim_fs_op *op = g_queue_peek_head(fs->op_q);
	const char *imsi = ofono_sim_get_imsi(fs->sim);
	enum ofono_sim_phase phase = ofono_sim_get_phase(fs->sim);
	enum sim_file_access update;
	enum sim_file_access invalidate;
	enum sim_file_access rehabilitate;
	unsigned char fileinfo[SIM_CACHE_HEADER_SIZE];
	gboolean cache;
	char *path;

	/* TS 11.11, Section 9.3 */
	update = file_access_condition_decode(access[0] & 0xf);
	rehabilitate = file_access_condition_decode((access[2] >> 4) & 0xf);
	invalidate = file_access_condition_decode(access[2] & 0xf);

	/* Never cache card holder writable files */
	cache = (update == SIM_FILE_ACCESS_ADM ||
			update == SIM_FILE_ACCESS_NEVER) &&
			(invalidate == SIM_FILE_ACCESS_ADM ||
				invalidate == SIM_FILE_ACCESS_NEVER) &&
			(rehabilitate == SIM_FILE_ACCESS_ADM ||
				rehabilitate == SIM_FILE_ACCESS_NEVER);

	if (imsi == NULL || phase == OFONO_SIM_PHASE_UNKNOWN || cache == FALSE)
		return;

	memset(fileinfo, 0, SIM_CACHE_HEADER_SIZE);

	fileinfo[0] = error->type;
	fileinfo[1] = length >> 8;
	fileinfo[2] = length & 0xff;
	fileinfo[3] = structure;
	fileinfo[4] = record_length >> 8;
	fileinfo[5] = record_length & 0xff;
	fileinfo[6] = file_status;

	path = g_strdup_printf(SIM_CACHE_PATH, imsi, phase, op->id);
	fs->fd = TFR(open(path, O_WRONLY | O_CREAT | O_TRUNC, SIM_CACHE_MODE));
	g_free(path);

	if (fs->fd == -1)
		return;

	if (TFR(write(fs->fd, fileinfo, SIM_CACHE_HEADER_SIZE)) ==
			SIM_CACHE_HEADER_SIZE)
		return;

	TFR(close(fs->fd));
	fs->fd = -1;
}

static void sim_fs_op_info_cb(const struct ofono_error *error, int length,
				enum ofono_sim_file_structure structure,
				int record_length,
				const unsigned char access[3],
				unsigned char file_status,
				void *data)
{
	struct sim_fs *fs = data;
	struct sim_fs_op *op = g_queue_peek_head(fs->op_q);

	if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
		sim_fs_op_error(fs);
		return;
	}

	sim_fs_op_cache_fileinfo(fs, error, length, structure, record_length,
					access, file_status);

	if (structure != op->structure) {
		ofono_error("Requested file structure differs from SIM: %x",
				op->id);
		sim_fs_op_error(fs);
		return;
	}

	if (op->cb == NULL) {
		sim_fs_end_current(fs);
		return;
	}

	op->structure = structure;
	op->length = length;

	if (structure == OFONO_SIM_FILE_STRUCTURE_TRANSPARENT) {
		if (op->num_bytes == 0)
			op->num_bytes = op->length;

		op->record_length = length;
		op->current = op->offset / 256;

		if (op->info_only == FALSE)
			fs->op_source = g_idle_add(sim_fs_op_read_block, fs);
	} else {
		op->record_length = record_length;
		op->current = 1;

		if (op->info_only == FALSE)
			fs->op_source = g_idle_add(sim_fs_op_read_record, fs);
	}

	if (op->info_only == TRUE) {
		/*
		 * It's an info-only request, so there is no need to request
		 * actual contents of the EF. Just return the EF-info.
		 */
		sim_fs_read_info_cb_t cb = op->cb;

		cb(1, file_status, op->length,
			op->record_length, op->userdata);

		sim_fs_end_current(fs);
	}
}

static gboolean sim_fs_op_check_cached(struct sim_fs *fs)
{
	const char *imsi = ofono_sim_get_imsi(fs->sim);
	enum ofono_sim_phase phase = ofono_sim_get_phase(fs->sim);
	struct sim_fs_op *op = g_queue_peek_head(fs->op_q);
	char *path;
	int fd;
	ssize_t len;
	unsigned char fileinfo[SIM_CACHE_HEADER_SIZE];
	int error_type;
	int file_length;
	enum ofono_sim_file_structure structure;
	int record_length;
	unsigned char file_status;

	if (imsi == NULL || phase == OFONO_SIM_PHASE_UNKNOWN)
		return FALSE;

	path = g_strdup_printf(SIM_CACHE_PATH, imsi, phase, op->id);

	if (path == NULL)
		return FALSE;

	fd = TFR(open(path, O_RDWR));
	g_free(path);

	if (fd == -1) {
		if (errno != ENOENT)
			DBG("Error %i opening cache file for "
					"fileid %04x, IMSI %s",
					errno, op->id, imsi);

		return FALSE;
	}

	len = TFR(read(fd, fileinfo, SIM_CACHE_HEADER_SIZE));

	if (len != SIM_CACHE_HEADER_SIZE)
		goto error;

	error_type = fileinfo[0];
	file_length = (fileinfo[1] << 8) | fileinfo[2];
	structure = fileinfo[3];
	record_length = (fileinfo[4] << 8) | fileinfo[5];
	file_status = fileinfo[6];

	if (structure == OFONO_SIM_FILE_STRUCTURE_TRANSPARENT)
		record_length = file_length;

	if (record_length == 0 || file_length < record_length)
		goto error;

	op->length = file_length;
	op->record_length = record_length;
	memcpy(fs->bitmap, fileinfo + SIM_FILE_INFO_SIZE,
			SIM_CACHE_HEADER_SIZE - SIM_FILE_INFO_SIZE);
	fs->fd = fd;

	if (error_type != OFONO_ERROR_TYPE_NO_ERROR ||
			structure != op->structure) {
		sim_fs_op_error(fs);
		return TRUE;
	}

	if (op->info_only == TRUE) {
		/*
		 * It's an info-only request, so there is no need to request
		 * actual contents of the EF. Just return the EF-info.
		 */
		sim_fs_read_info_cb_t cb = op->cb;

		cb(1, file_status, op->length,
			op->record_length, op->userdata);

		sim_fs_end_current(fs);
	} else if (structure == OFONO_SIM_FILE_STRUCTURE_TRANSPARENT) {
		if (op->num_bytes == 0)
			op->num_bytes = op->length;

		op->current = op->offset / 256;
		fs->op_source = g_idle_add(sim_fs_op_read_block, fs);
	} else {
		op->current = 1;
		fs->op_source = g_idle_add(sim_fs_op_read_record, fs);
	}

	return TRUE;

error:
	TFR(close(fd));
	return FALSE;
}

static gboolean sim_fs_op_next(gpointer user_data)
{
	struct sim_fs *fs = user_data;
	const struct ofono_sim_driver *driver = fs->driver;
	struct sim_fs_op *op;

	fs->op_source = 0;

	if (fs->op_q == NULL)
		return FALSE;

	op = g_queue_peek_head(fs->op_q);

	if (op->cb == NULL) {
		sim_fs_end_current(fs);
		return FALSE;
	}

	if (op->is_read == TRUE) {
		if (sim_fs_op_check_cached(fs))
			return FALSE;

		driver->read_file_info(fs->sim, op->id,
					op->path_len ? op->path : NULL,
					op->path_len,
					sim_fs_op_info_cb, fs);
	} else {
		switch (op->structure) {
		case OFONO_SIM_FILE_STRUCTURE_TRANSPARENT:
			driver->write_file_transparent(fs->sim, op->id, 0,
					op->length, op->buffer,
					NULL, 0, sim_fs_op_write_cb, fs);
			break;
		case OFONO_SIM_FILE_STRUCTURE_FIXED:
			driver->write_file_linear(fs->sim, op->id, op->current,
					op->length, op->buffer,
					NULL, 0, sim_fs_op_write_cb, fs);
			break;
		case OFONO_SIM_FILE_STRUCTURE_CYCLIC:
			driver->write_file_cyclic(fs->sim, op->id,
					op->length, op->buffer,
					NULL, 0, sim_fs_op_write_cb, fs);
			break;
		default:
			ofono_error("Unrecognized file structure, "
					"this can't happen");
		}

		g_free(op->buffer);
		op->buffer = NULL;
	}

	return FALSE;
}

int sim_fs_read_info(struct ofono_sim_context *context, int id,
			enum ofono_sim_file_structure expected_type,
			sim_fs_read_info_cb_t cb, void *data)
{
	struct sim_fs *fs = context->fs;
	struct sim_fs_op *op;

	if (cb == NULL)
		return -EINVAL;

	if (fs->driver == NULL)
		return -EINVAL;

	if (fs->driver->read_file_info == NULL)
		return -ENOSYS;

	if (fs->op_q == NULL)
		fs->op_q = g_queue_new();

	op = g_try_new0(struct sim_fs_op, 1);
	if (op == NULL)
		return -ENOMEM;

	op->id = id;
	op->structure = expected_type;
	op->cb = cb;
	op->userdata = data;
	op->is_read = TRUE;
	op->info_only = TRUE;
	op->context = context;

	g_queue_push_tail(fs->op_q, op);

	if (g_queue_get_length(fs->op_q) == 1)
		fs->op_source = g_idle_add(sim_fs_op_next, fs);

	return 0;
}

int sim_fs_read(struct ofono_sim_context *context, int id,
		enum ofono_sim_file_structure expected_type,
		unsigned short offset, unsigned short num_bytes,
		const unsigned char *path, unsigned int path_len,
		ofono_sim_file_read_cb_t cb, void *data)
{
	struct sim_fs *fs = context->fs;
	struct sim_fs_op *op;

	if (cb == NULL)
		return -EINVAL;

	if (fs->driver == NULL)
		return -EINVAL;

	if (fs->driver->read_file_info == NULL) {
		cb(0, 0, 0, NULL, 0, data);
		return -ENOSYS;
	}

	if (fs->op_q == NULL)
		fs->op_q = g_queue_new();

	op = g_try_new0(struct sim_fs_op, 1);
	if (op == NULL)
		return -ENOMEM;

	op->id = id;
	op->structure = expected_type;
	op->cb = cb;
	op->userdata = data;
	op->is_read = TRUE;
	op->offset = offset;
	op->num_bytes = num_bytes;
	op->info_only = FALSE;
	op->context = context;
	memcpy(op->path, path, path_len);
	op->path_len = path_len;

	g_queue_push_tail(fs->op_q, op);

	if (g_queue_get_length(fs->op_q) == 1)
		fs->op_source = g_idle_add(sim_fs_op_next, fs);

	return 0;
}

int sim_fs_write(struct ofono_sim_context *context, int id,
			ofono_sim_file_write_cb_t cb,
			enum ofono_sim_file_structure structure, int record,
			const unsigned char *data, int length, void *userdata)
{
	struct sim_fs *fs = context->fs;
	struct sim_fs_op *op;
	gconstpointer fn = NULL;

	if (cb == NULL)
		return -EINVAL;

	if (fs->driver == NULL)
		return -EINVAL;

	switch (structure) {
	case OFONO_SIM_FILE_STRUCTURE_TRANSPARENT:
		fn = fs->driver->write_file_transparent;
		break;
	case OFONO_SIM_FILE_STRUCTURE_FIXED:
		fn = fs->driver->write_file_linear;
		break;
	case OFONO_SIM_FILE_STRUCTURE_CYCLIC:
		fn = fs->driver->write_file_cyclic;
		break;
	default:
		ofono_error("Unrecognized file structure, this can't happen");
	}

	if (fn == NULL)
		return -ENOSYS;

	if (fs->op_q == NULL)
		fs->op_q = g_queue_new();

	op = g_try_new0(struct sim_fs_op, 1);
	if (op == NULL)
		return -ENOMEM;

	op->id = id;
	op->cb = cb;
	op->userdata = userdata;
	op->is_read = FALSE;
	op->buffer = g_memdup(data, length);
	op->structure = structure;
	op->length = length;
	op->current = record;
	op->context = context;

	g_queue_push_tail(fs->op_q, op);

	if (g_queue_get_length(fs->op_q) == 1)
		fs->op_source = g_idle_add(sim_fs_op_next, fs);

	return 0;
}

void sim_fs_cache_image(struct sim_fs *fs, const char *image, int id)
{
	const char *imsi;
	enum ofono_sim_phase phase;

	if (fs == NULL || image == NULL)
		return;

	imsi = ofono_sim_get_imsi(fs->sim);
	if (imsi == NULL)
		return;

	phase = ofono_sim_get_phase(fs->sim);
	if (phase == OFONO_SIM_PHASE_UNKNOWN)
		return;

	write_file((const unsigned char *) image, strlen(image),
			SIM_CACHE_MODE, SIM_IMAGE_CACHE_PATH, imsi,
			phase, id);
}

char *sim_fs_get_cached_image(struct sim_fs *fs, int id)
{
	const char *imsi;
	enum ofono_sim_phase phase;
	unsigned short image_length;
	int fd;
	char *buffer;
	char *path;
	int len;
	struct stat st_buf;

	if (fs == NULL)
		return NULL;

	imsi = ofono_sim_get_imsi(fs->sim);
	if (imsi == NULL)
		return NULL;

	phase = ofono_sim_get_phase(fs->sim);
	if (phase == OFONO_SIM_PHASE_UNKNOWN)
		return NULL;

	path = g_strdup_printf(SIM_IMAGE_CACHE_PATH, imsi, phase, id);

	TFR(stat(path, &st_buf));
	fd = TFR(open(path, O_RDONLY));
	g_free(path);

	if (fd < 0)
		return NULL;

	image_length = st_buf.st_size;
	buffer = g_try_new0(char, image_length + 1);

	if (buffer == NULL) {
		TFR(close(fd));
		return NULL;
	}

	len = TFR(read(fd, buffer, image_length));
	TFR(close(fd));

	if (len != image_length) {
		g_free(buffer);
		return NULL;
	}

	return buffer;
}

static void remove_cachefile(const char *imsi, enum ofono_sim_phase phase,
				const struct dirent *file)
{
	int id;
	char *path;

	if (file->d_type != DT_REG)
		return;

	if (sscanf(file->d_name, "%4x", &id) != 1)
		return;

	path = g_strdup_printf(SIM_CACHE_PATH, imsi, phase, id);
	remove(path);
	g_free(path);
}

static void remove_imagefile(const char *imsi, enum ofono_sim_phase phase,
				const struct dirent *file)
{
	int id;
	char *path;

	if (file->d_type != DT_REG)
		return;

	if (sscanf(file->d_name, "%d", &id) != 1)
		return;

	path = g_strdup_printf(SIM_IMAGE_CACHE_PATH, imsi, phase, id);
	remove(path);
	g_free(path);
}

void sim_fs_check_version(struct sim_fs *fs)
{
	const char *imsi = ofono_sim_get_imsi(fs->sim);
	enum ofono_sim_phase phase = ofono_sim_get_phase(fs->sim);
	unsigned char version;

	if (imsi == NULL || phase == OFONO_SIM_PHASE_UNKNOWN)
		return;

	if (read_file(&version, 1, SIM_CACHE_VERSION, imsi, phase) == 1)
		if (version == SIM_FS_VERSION)
			return;

	sim_fs_cache_flush(fs);

	version = SIM_FS_VERSION;
	write_file(&version, 1, SIM_CACHE_MODE, SIM_CACHE_VERSION, imsi, phase);
}

void sim_fs_cache_flush(struct sim_fs *fs)
{
	const char *imsi = ofono_sim_get_imsi(fs->sim);
	enum ofono_sim_phase phase = ofono_sim_get_phase(fs->sim);
	char *path = g_strdup_printf(SIM_CACHE_BASEPATH, imsi, phase);
	struct dirent **entries;
	int len = scandir(path, &entries, NULL, alphasort);

	g_free(path);

	if (len > 0) {
		/* Remove all file ids */
		while (len--) {
			remove_cachefile(imsi, phase, entries[len]);
			g_free(entries[len]);
		}

		g_free(entries);
	}

	sim_fs_image_cache_flush(fs);
}

void sim_fs_cache_flush_file(struct sim_fs *fs, int id)
{
	const char *imsi = ofono_sim_get_imsi(fs->sim);
	enum ofono_sim_phase phase = ofono_sim_get_phase(fs->sim);
	char *path = g_strdup_printf(SIM_CACHE_PATH, imsi, phase, id);

	remove(path);
	g_free(path);
}

void sim_fs_image_cache_flush(struct sim_fs *fs)
{
	const char *imsi = ofono_sim_get_imsi(fs->sim);
	enum ofono_sim_phase phase = ofono_sim_get_phase(fs->sim);
	char *path = g_strdup_printf(SIM_IMAGE_CACHE_BASEPATH, imsi, phase);
	struct dirent **entries;
	int len = scandir(path, &entries, NULL, alphasort);

	g_free(path);

	if (len <= 0)
		return;

	/* Remove everything */
	while (len--) {
		remove_imagefile(imsi, phase, entries[len]);
		g_free(entries[len]);
	}

	g_free(entries);
}

void sim_fs_image_cache_flush_file(struct sim_fs *fs, int id)
{
	const char *imsi = ofono_sim_get_imsi(fs->sim);
	enum ofono_sim_phase phase = ofono_sim_get_phase(fs->sim);
	char *path = g_strdup_printf(SIM_IMAGE_CACHE_PATH, imsi, phase, id);

	remove(path);
	g_free(path);
}